kamal
kamal

Reputation: 9785

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

I have a loop where i execute a series of command on a remote machine:


   ssh.exec('cd /vmfs/volumes/4c6d95d2-b1923d5d-4dd7-f4ce46baaadc/ghettoVCB;  ./ghettoVCB.sh -f vms_to_backup -d dryrun') do|ch, stream, data|
                                    if #{stream} =~ /vmupgrade/
                                    puts value_hosts + " is " + data
                                    puts #{stream}
                                    puts data
                                    end
                            end

i want to access #{stream} and data outside the do-end loop

I would appreciate any help. Thanks,

Hi Jörg,

I implemented your suggestions, but now i am getting error:


WrapperghettoVCB.rb:49: odd number list for Hash
      communicator = {ch: ch, stream: stream, data: data}
                         ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '}'
      communicator = {ch: ch, stream: stream, data: data}
                         ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '='
      communicator = {ch: ch, stream: stream, data: data}
                                     ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '='
      communicator = {ch: ch, stream: stream, data: data}
                                                   ^
WrapperghettoVCB.rb:76: syntax error, unexpected kELSE, expecting kEND
WrapperghettoVCB.rb:80: syntax error, unexpected '}', expecting kEND

Upvotes: 10

Views: 15075

Answers (2)

Jörg W Mittag
Jörg W Mittag

Reputation: 369518

You can't. Local variables are local to their scope. That's why they are called local variables.

You could, however, use a variable from an outer scope:

communicator = nil

ssh.exec('...') do |ch, stream, data|
  break unless stream =~ /vmupgrade/
  puts "#{value_hosts} is #{data}", stream, data
  communicator = {ch: ch, stream: stream, data: data}
end

puts communicator

BTW: there were several bugs in your code, that would have prevented it from working anyway regardless of your problem with variable scoping, because you used wrong syntax for dereferencing local variables: the syntax for dereferencing a variable is simply the name of the variable, e.g. foo, and not #{foo} (that's simply a syntax error).

Also, there are some other improvements:

  • formatting: the standard for indentation in Ruby is 2 spaces, not 26
  • formatting: the standard for indentation in Ruby is 2 spaces, not 0
  • formatting: usually, the block arguments are separated from the do keyword with a space
  • guard clause: if you wrap the entire body of a block or method in a conditional, you can just replace that with a guard a the beginning of the block that skips the whole block if the condition is true
  • string interpolation: adding strings together with + is unusual in Ruby; if you need to concatenate strings, you usually do it with <<, but more often than not, string interpolation is preferred
  • multiple arguments to puts: if you pass multiple arguments to puts, it will print all of them on a separate line, you don't need to call it multiple times

Upvotes: 30

DigitalRoss
DigitalRoss

Reputation: 146123

c, s, d = [nil] * 3
str = '...'
ssh.exec str do |ch, stream, data|
  c, s, d = ch, stream, data
  if #{stream} =~ /vmupgrade/
    puts value_hosts + " is " + data
    puts #{stream}
    puts data
  end
end

Someone may suggest that you just reference outer scope variables as the block parameters, but the scoping of block parameter names has changed in Ruby recently and I would suggest playing it safe and just doing it this way.

I don't understand what's going on in the snippet but the general design pattern is often used to yield handles to OS objects and things that are automatically shut down / closed / etc after the block is done, so retaining the Ruby object wrapper may not be useful.

Upvotes: 5

Related Questions