From: Rick DeNatale Date: 2009-08-27T01:26:44+09:00 Subject: Re: ||= with 1.8 and 1.9 ? On Wed, Aug 26, 2009 at 12:15 PM, 7stud -- wrote: > If so, then David Black didn't know about it: > > ------- > WARNING   Setter methods don't return what you might think.  When you > use the syntactic sugar that lets you make calls to = methods look like > assignments, Ruby takes the assignment semantics seriously.  Assignments > (like x = 1) evaluate to whatever's on their right-hand side.  [On the > other hand,] [m]ethods usually return the value of the last expression > evaluated during execution.  But = method calls behave like assignments: > the value of the expression ticket.price = 63.00 is 63.00, even if the > ticket= method returns the string "Ha ha!"  The idea is to keep the > semantics consistent.  Under the hood, it's a method call; but it looks > like an assignment and behaves like an assignment with respect to its > value as an expression. > ------ > p. 72, The Well Grounded Rubyist I know what David is getting at here, but that first sentence isn't exactly true, although I'll grant a pedagogical license for it. It's not that the return value isn't what you think, it's that it's ignored when the setter method is called from the sugary syntax of an assignment. Note that he says that ticket.price = 63.00 evaluates to 63.00 "even if the ticket= method returns the string "Ha Ha!". Which is different than saying ticket= returns 63.00 instead of "what you might think." And Brian's example of using send demonstrates that it's the assignment and not the method call result which isn't what you think. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale