我有一个使用 attr_accessor 的对象。我希望能够使用带有 的变量调用该对象上的方法#send
。我的问题是该=
方法似乎不起作用。
class Foo
attr_accessor :bar
end
class Test_class
def test_method(x)
f = Foo.new
f.send(x)
f.send(x) = "test" #this doesnt work
f.send("#{x} =") "test" #this also doesn't work
# How can I set bar?
end
end
t = Test_class.new
t.test_method("bar")