From: Andreas S Date: 2007-01-03T14:28:59+09:00 Subject: Re: Pickaxe question: "... on the way to true Ruby mastery." My guess is it is necessary to keep the value in the array in case the real value is nil. Without it in an array, @__#{id.to_i}__ will evaluate to nil, and the || operator will execute the next term, the function call. You don't want to call the function again, your desired value is already in the instance variable and it is nil. (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0] -andre >From: jeffz_2002@yahoo.com >Reply-To: ruby-talk@ruby-lang.org >To: ruby-talk@ruby-lang.org (ruby-talk ML) >Subject: Pickaxe question: "... on the way to true Ruby mastery." >Date: Wed, 3 Jan 2007 13:55:08 +0900 > >Pickaxe has this code on page 374: > >def once(*ids) # :nodoc: > for id in ids > module_eval <<-"end;" > alias_method :__#{id.to_i}__, :#{id.to_s} > private :__#{id.to_i}__ > def #{id.to_s}(*args, &block) > (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0] > end >end; >end >end > >followed by "Understand this code, and you'll be well on the way to >true Ruby mastery." I don't, so I'm not, but would like to, and then I >will (according to the book) - with some help. > >I'm good up until the line (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, >&block)])[0] > >Say the id.to_i was 42 ... then this reads: (@__42__ ||= [__42__(*args, >&block)])[ 0 ] > >or: (@__42__ ||= [ return value from __42__ method call ] )[ 0 ] > > >Question: Why can't we just write > >(@__42__ ||= return value from __42__ method call) > >Why is the [0] necessary at all? > >Thanks, (ps - a link to an in-depth explanation would be sufficient!) > >Jeff > > _________________________________________________________________ Get live scores and news about your team: Add the Live.com Football Page www.live.com/?addtemplate=football&icid=T001MSN30A0701