在Ruby中,可以显式创建局部变量

bdi*_*ych 3 ruby variables block local proc

例如

x = 123
p = Proc.new {
  x = 'I do not want change the value of the outer x, I want to create a local x'
}
Run Code Online (Sandbox Code Playgroud)

在Ruby中是否有与Perl中的"my"关键字相同的东西?

Aru*_*hit 6

根据Perl文档,我认为你在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
Run Code Online (Sandbox Code Playgroud)