ruby, define []= operator, why can't control return value?

Viewed 2024

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?

2 Answers
Related