在Ruby中,可以显式创建局部变量 [英] In Ruby is it possible to create a local variable explicitly

查看:112
本文介绍了在Ruby中,可以显式创建局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

x = 123
p = Proc.new {
  x = 'I do not want change the value of the outer x, I want to create a local x'
}

在Ruby中,Perl中有与"my"关键字相同的内容吗?

In Ruby Is there something the same as "my" keyword in Perl ?

推荐答案

根据Perl 文档"rel =" nofollow> ,我认为您正在Ruby中寻找以下内容:-

As per the Perl documentation of my,I think you are looking for something like below in Ruby:-

x = 123 
p = Proc.new {|;x|  
  x = 'I do not want change the value of the outer x, I want to create a local x'
}
p.call 
# => "I do not want change the value of the outer x, I want to create a local x"
x # => 123

这篇关于在Ruby中,可以显式创建局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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