Why does irb echo the right hand side of an assignment instead of the return value in the case of a setter method?

Frank Joseph Mattia

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?

Arup Rakshit

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.

edited at
0

Comments

0 comments
Login to comment

Related

does subscript evaluate before the right hand side of assignment operator

Is the result of an assignment expression always the value on the right-hand side?

Attribute is not identical to the right hand side value of a preceding attribute assignment operation

*why* does list assignment flatten its left hand side?

Is 'or' used on the right-hand-side of an assignment pythonic?

TypeScript inconsistency - why does compiler allow object property value assignment but no method return value assignment?

Why is this an invalid assignment left hand side? - 2.0

Why is this an invalid assignment left hand side?

Why does Python assignment not return a value?

Erlang: no match of right hand side value

"Deconstruct assignment requires an expression with a type on the right hand side"

Different slicing behaviors on left/right hand side of assignment operator

Tilde not expanded when quoting on the right hand side of a Bash variable assignment

Why does this method not return a value?

Why does the right-hand-side of clause not agree with function result type?

why does python pep526 accept literals as the right hand side argument?

Why does defining a method called ! break IRB?

Why does axios return promise instead of the value?

Why can't the left hand side of an assignment be an increment expression?

Why I get "Invalid left-hand side in assignment"?

What does this 'or' do on the left hand side of a PHP assignment statement?

How does this left hand side assignment for a function call work in JScript?

What does Some() do on the left hand side of a variable assignment?

Conditionally choose a value on the left hand side of an assignment statement?

Isabelle theory 'Variable "return" occurs on right hand side only'

Why does "=>" assignment work in this case but not "="?

Why Diamond operator was not missed from Right hand side in Java 7?

Why are there spaces on the left and right hand side of my seekbar?

Why is my Stack clipping the right-hand side of images?