You want f.send "#{x}=", "test"
. In Ruby, method names may include punctuation, such as =
or !
. The methods created by attr_accessor :bar
are simply named bar
and bar=
. In fact, attr_accessor :bar
is just shorthand for:
def bar
@bar
end
def bar=(value)
@bar = value
end
When you're calling foo.bar = "baz"
, you're actually calling the #bar=
method with foo
as the receiver and "bar"
as the first parameter to the function - that is, foo.bar=("baz")
. Ruby just provides syntactic sugar for methods ending in =
so that you can write the more natural-looking form.