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