如何动态创建局部变量?我有一个变量var = "some_name"我想要创建一个新的对象并将它分配给some_name..我该怎么做?例如:var = "some_name"some_name = Struct.new(:name) # I need thisa = some_name.new('blah') # so that I can do this.
3 回答
HUH函数
TA贡献1836条经验 获得超4个赞
eval
):
eval 'foo = "bar"'foo # NameError: undefined local variable or method `foo' for main:Object
eval 'foo = "bar"; foo + "baz"'#=> "barbaz"
local_variable_set
binding.local_variable_set :foo, 'bar'foo # NameError: undefined local variable or method `foo' for main:Object
hash = {}hash[:my_var] = :foo
eval
local_variable_set
做
foo = nileval 'foo = "bar"'foo #=> "bar"binding.local_variable_set :foo, 'baz'foo #=> "baz"
- 3 回答
- 0 关注
- 649 浏览
添加回答
举报
0/150
提交
取消