From: dblack@... Date: 2007-01-03T18:20:59+09:00 Subject: Re: Pickaxe question: "... on the way to true Ruby mastery." Hi -- On Wed, 3 Jan 2007, jeffz_2002@yahoo.com wrote: > Hey Andre, I believe the line reads slightly differently than you've > interpreted ... some more info (note: I still don't have the answer): > > ||= is actually a ruby idiom to initialize a variable if it's nil. In > other words, "a ||= 6" actually means "a = 6 if a.nil?" or "a = 6 > unless a". (sorry if I'm lecturing) In other words, if @foo really > *is* a value, and it really *is* nil, I think the function call is > going to be made no matter what ... but again, this is a bit over my > head at present. > > So, converting __#{id.to_i}__ to something more readable, like "foo", > this: (@foo ||= [foo(*a, &b)] )[ 0 ] > > should be the same as the more verbose: > > if @foo > return @foo[ 0 ] # Still not sure why the [0] is required! > else > return foo( *a, &b ) # the 0th index of the single array > end It's more like: if @foo return @foo[0] else @foo = [foo(*a, &b)] return @foo[0] end or, more simply, @foo = [foo(*a, &b)] unless @foo return @foo[0] In other words, @foo gets assigned to, if it's nil to start with. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)