From: Kenosis Date: 2007-01-04T07:55:08+09:00 Subject: Re: Pickaxe question: "... on the way to true Ruby mastery." jeffz_2002@yahoo.com wrote: > Thanks David and Andre, I finally get it, and Andre, you're right about > the nil/false idea in your response to my reponse. > > For anyone else following this thread, the idea is: > > - The first time the method is called, @foo doesn't exist. > > - @foo doesn't exist, so it's assigned a single-element array > containing the value returned by the call to the private aliased > original method. This value can be anything, including an array, > false, nil, whatever. > > - The 0th array element, which is just the return value of the function > call, is returned. > > - The next time the method is called, @foo still exists, and is still a > single-element array. We again return the 0th element, which is the > answer. > > The [0] is used because the result is stored in an array ... this gets > around the original method returning nil or false, which would end up > in a second call to the function, had we just naively used @foo ||= > foo( *args, &block ).. > > Thanks again. It's pretty obvious, actually ... sigh. > > Funny, I still don't feel well on my way to mastery. I actually feel > dumber, in a way. > > jz Welcome to the wonderful world of meta-programming in Ruby! And don't feel dumb. As the saying goes: the more you know, the more you know that you don't know. Ask any true master of anything and that'll be the answer they give you; if they're a >true< master. Ken PS. Check these out to continue your mastery... http://www.erikveen.dds.nl/monitorfunctions/index.html http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/22b97167fe614efc/2627d192d2bf56d5?hl=en#2627d192d2bf56d5