From: Ilias Lazaridis Date: 2005-04-03T22:04:43+09:00 Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Anders Engstr�m wrote: > On Sun, Apr 03, 2005 at 08:39:42PM +0900, Ilias Lazaridis wrote: > [snipp] > >>But to understand this fully, can someone please decrypt this: >> >> def meta() >> @meta ||= {} >> end > > That's lazy initialization of the @meta variable. Another (and more > verbose) way to write it would be: > > def meta() > unless @meta #If not defined, set the value > @meta = {} > end > @meta # Return Meta > end def meta() # use of "()" is optional unless @meta # if @meta == nil (unless = if not) @meta = {} # creates a new hash end @meta # returns the (existing or new) hash end > "@meta ||= {}" evaluates to "@meta = @meta || {}" and since the returned "||=" similar to "+=" "||" means: if left operand is nil, pick right operand. > value from a method is the last evaluated expression "@meta ||= {}" > works. ok, got it! [will update the document soon - and hopefully can soon take the next round] > //Anders .. -- http://lazaridis.com