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) ^
An instance variable must start with a @ (“at” sign or commercial at). Otherwise instance variable names follow the rules as local variable names. Since the instance variable starts with an @ the second character may be an upper-case letter. An uninitialized instance variable has a value of nil .
Ruby doesn't have any concept of a pure, non-reference value, so you certainly can't pass one to a method. Variables are always references to objects.
Ruby Language Metaprogramming send() methodsend() is used to pass message to object . send() is an instance method of the Object class. The first argument in send() is the message that you're sending to the object - that is, the name of a method. It could be string or symbol but symbols are preferred.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With