From: "Jesús Gabriel y Galán" Date: 2011-08-26T00:23:32+09:00 Subject: Re: New to Ruby some problems On Thu, Aug 25, 2011 at 4:57 PM, jack jones wrote: > thank you all very much :) I just wanted to point out something that I think hasn't been mentioned. In the "assignment-like" methods, where Ruby applies syntactic sugar so that you can use the usual syntax, Ruby also does something that is not the same as with other method calls: the result of the "assignment" expression is the expression's right hand side, not the result value of the method: ruby-1.8.7-p334 :001 > class A ruby-1.8.7-p334 :002?> def []= a,b ruby-1.8.7-p334 :003?> @value = a + b ruby-1.8.7-p334 :004?> @value ruby-1.8.7-p334 :005?> end ruby-1.8.7-p334 :006?> end => nil ruby-1.8.7-p334 :007 > a = A.new => # ruby-1.8.7-p334 :008 > a[3] = 4 => 4 ruby-1.8.7-p334 :009 > a => # Note that the value of the expression in line 8 is 4 (as shown by IRB) and not 7, which is the value returned by the method []= If you use the non-syntactic sugar version, though, the return value is whatever the method returns: ruby-1.8.7-p334 :010 > a.[]=(3,4) => 7 This is usually not a problem, since for this to bite you, you need to be doing something in the []= and trying to use that return value outside, not very likely. But anyway, I thought it was worth mentioning. Jesus.