11

Consider the following code snippet:

class Example
  def my_attr=(value)
    @_my_attr = value
    @_my_attr * 3
  end
end

I expect the expression Example.new.my_attr = 5 to return 15, but that turns out to be wrong. The original return value is always returned, even when I call the = method explicitly:

Example.new.my_attr = 5 # => 5
Example.new.my_attr=(5) # => 5

How and why does Ruby do this? Does Ruby treat methods that end in = specially, or is it some other mechanism? I guess this precludes chaining on return values of = methods, right? Is there a way to make Ruby behave differently, or is this just how it is?

Update: Credit to @jeffgran for this:

Example.new.send(:my_attr=, 5) # => 15

This is a workaround, but on another level even more perplexing, since that would mean send is clearly not always equivalent in behavior to calling a method directly.

4

1 Answer 1

22

This is how assignment works; the return value is ignored, and the result of an assignment expression is always the right-hand value. This is a fundamental feature of Ruby's grammar. left-hand side = right-hand side will always evaluate to right-hand side, regardless of whether left hand side is a variable (x), a method (object.x), a constant (X) or any expression.

Source: Programming Languages | Ruby IPA Ruby Standardization WG Draft, 11.4.2.2.5, Single method assignments


Consider chaining of assignments, x = y = 3.

For this to work correctly, the result of y = 3 must be 3, regardless of the actual value returned by the y= method. x = y = 3 is meant to read as y = 3; x = 3, not as y = 3; x = y which is what would be implied if the return value from y= was treated as the result of y = 3.

Or consider all the other places assignment can be used. Sometimes, instead of this...

obj.x = getExpensiveThing()
if obj.x 
  ...

... we write this ...

if obj.x = getExpensiveThing()

This couldn't work if the result of obj.x = ... could be any arbitrary thing, but we know it will work because the result of obj.x = y is always y.

Update

A comment on the question states:

Interesting, I wasn't aware of this scenario. It seems that method= returns whatever input is given...

No, it's an important distinction to make. This has nothing to do with the return value of method assignment, and it definitely does not "return whatever input is given", it returns whatever you tell it to return.

The whole point is that the return value is ignored by the grammar of the language; assignment doesn't evaluate to the return value of the attr= method, but the return value still exists as evidenced by the question itself: Example.new.send(:my_attr=, 5) # => 15. This works because it is not assignment. You're side-stepping that part of the Ruby language.

Update again

To be clear: x and y in my examples shouldn't be interpreted as literal Ruby variables, they are place holders for any valid left-hand side of an assignment. x or y could be any expression: a, obj.a, CONSTANT_A, Something::a, @instance_a, it's all the same. The value of assignment is always the right-hand side.

6
  • 1
    Curious about the down-vote? Is it because "that's how it works" is not a satisfying answer? Unfortunately, this is the answer; that's how Ruby works. It was not an arbitrary decision, it's required for statements like x = y = 3 to work.
    – user229044
    Sep 26, 2013 at 17:03
  • 2
    Assignment and setter methods are different. If you do y = 3 (without any method definition in advance), then there is no such thing as "the y= method" that you wrote. That is an assignment. The OP's point is about setter methods, not assignments. Assignment is irrelevant. They are clearly distinguished syntactically. A setter method always requires an explicit receiver. Assignment never appears with a receiver. However, it is possibile that setter methods were designed to behave like assignments because they appear to be similar at the surface.
    – sawa
    Sep 26, 2013 at 17:26
  • @sawa Assignment is assignment, and it always evaluates to the right-hand side of the expression. There is some syntactic sugar that invokes a method= method if the left-hand side of the assignment matches such a method, but that doesn't change the way assignment works.
    – user229044
    Sep 26, 2013 at 17:28
  • So that would suggest that method= can only ever be used for side-effects, like setting an instance variable in the receiver. If the code in 'method=' doesn't have side effects, it just wastes CPU and time and has no other effect. Also, we can call the method and get the expected return value with 'send'. Does this mean that Ruby looks for method definitions ending in the '=' character, and except in the case of 'send', intercepts them so that the rvalue is returned? Sep 26, 2013 at 17:32
  • @door_number_three No, it has nothing to do with the method name, it has everything to do with how the assignment works at a language level. Assignment expressions evaluate to the right-hand value.
    – user229044
    Sep 26, 2013 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.