From: Eric Mahurin Date: 2005-10-19T07:03:54+09:00 Subject: Re: The Ruby Way to build an object unless nil? --- Kevin Ballard wrote: > Joby Bednar wrote: > > class NilClass > > def method_missing(method, *args) > > nil > > end > > end > > > > Forget the ladder... use the elevator! ;) > > While that certainly works, I would advise not doing that. > The problem > with this is it will hide problems that might show up > elsewhere in your > code. I also think you shouldn't overload the semantics of "nil" anymore. It already has enough meaning. Use another object (like below), or just check for nil explicitly like most other do. What I do when I need this kind of functionality is just make a "do nothing" object that just returns self for everything. Then your callers just pass this when they want to do nothing for that object: Nothing = Class.new { def method_missing(method, *args, &block) self end }.new An example is that I have a method that takes a buffer object where I store results. Sometimes I don't need the buffer, so I just pass it something like the above. __________________________________ Yahoo! Music Unlimited Access over 1 million songs. Try it free. http://music.yahoo.com/unlimited/