Ruby, How can i access local variables outside the do - end loop -


I have a loop where I execute a series of commands on the remote machine:

 < Code> Ssh.exec ('cd / vmfs / volume / 4c6d95d2-b1923d5d-4dd7-f4ce46baaadc / ghettoVCB ;./ghettoVCB.sh -f vms_to_backup -d swather'). Ch, stream, data | If # {stream} = ~ / vmupgrade / puts value_hosts + "is" + puts data # {stream} data ends ending in the end  

I use # {stream} and data I want end-end loop

I appreciate any help thank you,

Hi Jarg,

I have applied your suggestions, but now I Error getting:

  WrapperghettoVCB.rb: 49: Odd number list hashcommunicator = {ch: ch, stream: stream, data: data} ^ For syntax error, syntax error Error, unexpected ':',: 'syntax error, unexpected': 'error', 'unexpected': 'expectation'} 'communicator = {ch: ch, stream: stream, data: data} ^ WrapperghettoVCB.rb: 49: syntax error, Hopefully '=' communicator = {ch: ch, stream: stream, data: data} is expected that '=' communicator = {ch: ch, stream: stream, data: data} ^ wrappergettvcb RB: 76: Syntax error, unexpected KLE, hoping KEND WrapperghettoVCB.rb: 80: Syntax error, unexpected '}', expected KEND  

You can not local localities are local in their area, which is why they are called local variables .

However, you can use a variable from an external area:

  communicator = nil ssh Exec ('...'). Ch, stream, data | Stream = ~ / vmupgrade / puts "# {value_hosts} # # {data}" stream, data communicator = {ch: ch, stream: stream, data: data} puts the end communicator until a break occurs  

BTW: There were many bugs in your code, which could prevent it from working with extreme scoping regardless of your problem anyway Used wrong syntax for defering: syntax for a dereferencing variable Just the name of the variable, eg foo , and not # {foo} (this is just a syntax error).

In addition, there are some other improvements:

  • formatting : there are 2 standard spaces for indentation in Ruby, not 26
  • Formatting : Normally, the block argument is a < / Code> is different from the keyword
  • Guard Clause : If you wrap the body of a block or method conditionally , you can only do it The beginning of the block which can change with a guard Leaves the entire block if the condition is true
  • string launcher : adding the wire with + is unusual in Ruby; If you want to add a string, you usually call it & lt; & Lt; , but more often than not, string launches are preferred
  • puts multiple codes : if you If you pass several arguments for puts , then it will print all of them on a separate line, you do not need to call it

Comments