10

Trying to do something weird that might turn into something more useful, I tried to define my own []= operator on a custom class, which you can do, and have it return something different than the value argument, which apparently you can't do. []= operator's return value is always value; even when you override this operator, you don't get to control the return value.

class Weird 
  def []=(key, value)
    puts "#{key}:#{value}"
    return 42
  end
end

x = Weird.new
x[:a] = "a"
  output "a:a"
  return value => "a"  # why not 42?

Does anyone have an explanation for this? Any way around it?

ruby MRI 1.8.7. Is this the same in all rubys; Is it part of the language?

0

2 Answers 2

9

Note that this behavior also applies to all assignment expressions (i.e. also attribute assignment methods: def a=(value); 42; end).

My guess is that it is designed this way to make it easy to accurately understand assignment expressions used as parts of other expressions.

For example, it is reasonable to expect x = y.a = z[4] = 2 to:

  1. call z.[]=(4,2), then
  2. call y.a=(2), then
  3. assign 2 to the local variable x, then finally
  4. yield the value 2 to any “surrounding” (or lower precedence) expression.

This follows the principle of least surprise; it would be rather surprising if, instead, it ended up being equivalent to x = y.a=(z.[]=(4,2)) (with the final value being influenced by both method calls).


While not exactly authoritative, here is what Programming Ruby has to say:

  • Programming Ruby (1.8), in the Expressions section:

    An assignment statement sets the variable or attribute on its left side (the lvalue) to refer to the value on the right (the rvalue). It then returns that value as the result of the assignment expression.

  • Programming Ruby 1.9 (3rd ed) in section 22.6 Expressions, Conditionals, and Loops:

    (right after describing []= method calls)

    The value of an assignment expression is its rvalue. This is true even if the assignment is to an attribute method that returns something different.

1
  • 2
    Note that this is not true when using #send() to invoke assignment methods.
    – fny
    Sep 20, 2013 at 14:54
5

It’s an assignment statement, and those always evaluate to the assigned value. Making this different would be weird.

I suppose you could use x.[]= :a, "a" to capture the return value.

4
  • why would it be weird? Hmm, but okay then, i guess that's sort of an answer. I was trying something weird and experimental where something that looked like a nested bunch of Hash's would actually be a sort of arel-style closure-under-composition thing, that let you chain together modifications that weren't actually applied until a later date. Apparently not possible, at least using default syntax, which was kind of the point, to make it invisible. oh well.
    – jrochkind
    Jun 16, 2011 at 3:53
  • "Making this different would be weird.", and that's my problem with what is being asked by the OP. It rapidly becomes a maintenance nightmare to change the expected behavior of operators. This is one of those things I'd criticize in a code review. Jun 16, 2011 at 3:53
  • It would also be a pain to insure that all setters always return the right value. Note that x.send :[]=, :a, "a" will also return 42. Jun 16, 2011 at 5:42
  • 3
    Nitpick: it's not an assignment statement, it's an assignment expression. (If if were an assignment statement, then it wouldn't evaluate to anything, since that's pretty much the definition of "statement".) In fact, Ruby doesn't have statements at all, everything is an expression. Jun 16, 2011 at 14:26

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.