Matias Barrios
Matias Barrios

Reputation: 5056

Process throwing "Unexpected return" error

I am trying to create a Chef recipe that will read attributes from a Role, and one of those attributes will contain Bash code that needs to be executed on the server.

Hence, I created the below process:

def proc_test(command)
  proc = Proc.new { |command| command_out = Mixlib::ShellOut.new(command) ; command_out.run_command ; return command_out.stdout + command_out.stderr  }
  proc.call(command)
end

which, when run like this:

node.set['attributes']['nested_attribute'] = proc_test("hostname")

works. But if I attempt to do this:

command="hostname"
node.set['attributes']['nested_attribute'] = proc_test(command)

it throws this error:

LocalJumpError
--------------
unexpected return

Can someone help me with this?

If, instead of using the return keyword, I use put, I get the same error.

Upvotes: 0

Views: 246

Answers (2)

coderanger
coderanger

Reputation: 54211

I don't understand why you think you need a Proc at all. Just use the normal shell_out! helper method or a bash resource.

Upvotes: 1

Igor Drozdov
Igor Drozdov

Reputation: 15045

The problem is that return can't be called from Ruby procs. Just skip return to avoid the error:

 def proc_test(command)
     proc = Proc.new do |command|
       command_out = Mixlib::ShellOut.new(command) 
       command_out.run_command
       command_out.stdout + command_out.stderr
     end

     proc.call(command)
 end

Upvotes: 0

Related Questions