7 ms·
If you do ruby for any length of time, you’ll have run into assignment actually being a method call. object.attr = 1 is object.send(:attr=, 1) And yes, sett
by kayodelycaon 4y ago
If you do ruby for any length of time, you’ll have run into assignment actually being a method call.
object.attr = 1
is
object.send(:attr=, 1)
And yes, setters can take multiple arguments. They are just methods. Nothing special about them.
- yxhuvud 4y agoNot true, there is special things about them, like always returning their argument.
- byroot 4y agoYou are confusing setters with the assignment evaluation. Setters are just normal methods. ?> def you_are_wrong=(a) ?> a * 2 >> end => :you_are_wrong= >> send(:you_are_wrong=, 24) => 48 However yes, the `foo.bar = 24` expressions translates to `(foo.send(:bar=, 24); 24)`. The reason is so that `foo = bar.baz = 12` works as you expects, it's absolutely not some kind of cruft or black magic.