Ruby,我如何在 do-end 循环之外访问局部变量 [英] Ruby, How can i access local variables outside the do - end loop

查看:55
本文介绍了Ruby,我如何在 do-end 循环之外访问局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,我在远程机器上执行一系列命令:

<预><代码>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 " + dataputs #{stream}放数据结尾结尾

我想在 do-end 循环之外访问 #{stream} 和数据

我将不胜感激.谢谢,

嗨约尔格,

我实施了您的建议,但现在出现错误:

<预><代码>WrapperghettoVCB.rb:49:哈希的奇数列表通信器 = {ch:ch,流:流,数据:数据}^WrapperghettoVCB.rb:49: 语法错误,意外的 ':',期待 '}'通信器 = {ch:ch,流:流,数据:数据}^WrapperghettoVCB.rb:49: 语法错误,意外的:",期待="通信器 = {ch:ch,流:流,数据:数据}^WrapperghettoVCB.rb:49: 语法错误,意外的:",期待="通信器 = {ch:ch,流:流,数据:数据}^WrapperghettoVCB.rb:76: 语法错误,意外的 kELSE,期待 kENDWrapperghettoVCB.rb:80: 语法错误,意外的}",期待 kEND

解决方案

你不能.局部变量是其作用域的局部变量.这就是为什么它们被称为局部变量.

但是,您可以使用外部作用域中的变量:

communicator = nilssh.exec('...') 做 |ch、流、数据|除非流 =~/vmupgrade/puts "#{value_hosts} is #{data}", 流, 数据通信器 = {ch:ch,流:流,数据:数据}结尾把通讯器

顺便说一句:你的代码中有几个错误,不管你的变量作用域有什么问题,它都会阻止它无论如何工作,因为你使用了错误的语法来取消引用局部变量:取消引用的语法变量只是变量的名称,例如foo,而不是 #{foo}(这只是一个语法错误).

此外,还有一些其他改进:

  • 格式:Ruby 中缩进的标准是 2 个空格,而不是 26
  • 格式:Ruby 中缩进的标准是 2 个空格,而不是 0
  • 格式:通常,块参数与do关键字之间用空格分隔
  • guard 子句:如果将块或方法的整个主体包装在条件中,则可以将其替换为块的开头的保护如果条件为真,则跳过整个块
  • 字符串插值:在Ruby中用+添加字符串是不常见的;如果你需要连接字符串,你通常用<<来做,但通常情况下,字符串插值是首选
  • puts 的多个参数:如果您将多个参数传递给 puts,它将在单独的一行中打印所有参数,您不需要不需要多次调用它

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

解决方案

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

这篇关于Ruby,我如何在 do-end 循环之外访问局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆