It seems surprising that in all other instances irb will echo the return value of a method. Why does assignment via a setter behave differently?
I'm using Ruby 2.2.2.
irb(main):001:0> def x=(value); puts "puts_from_x"; "returned_string_from_x"; end
=> nil
irb(main):002:0> self.x = 3
puts_from_x
=> 3
update
It has dawned on me that it echoes the rhs because that's the actual return value. Why is this?
Following the @Matz reply in this thread :
Setters always return the value they were originally assigned It's a design choice. We defined the value of the assignment as the value of the right hand expression, not the return value from the assigning method.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments