Sergey Sergey - 4 months ago 7
Ruby Question

Ruby: using Object.send for assigning variables

Is there any way to do something like this?

a = Struct.new(:c).new(1)
b = Struct.new(:c).new(2)

a.send(:c)
=> 1

b.send(:c)
=> 2

a.send(:c) = b.send(:c)


The last line result in error:

syntax error, unexpected '=', expecting $end
a.send(:c) = b.send(:c)
^

Answer
a.send(:c=, b.send(:c))

foo.bar = baz isn't a call to the method bar followed by an assignment - it's a call to the method bar=. So you need to tell send to call that method.