From: Trans Date: 2007-05-26T01:05:43+09:00 Subject: Re: Replacement idiom for "list_or_nil.to_a"? On May 25, 8:05 am, "Chris Carter" wrote: > On 5/25/07, Robert Klemme wrote: > > > irb(main):001:0> class NilClass > > irb(main):002:1> def each; self; end > > irb(main):003:1> include Enumerable > > irb(main):004:1> end > > => NilClass > > irb(main):005:0> nil.each {|x| puts x} > > => nil > > irb(main):006:0> > > That hack will also make nil.to_a => []. If the code will be reusable, that's a little too risky. It would be interesting however for an object to exist, say Void. Something like: class Void def method_missing(*args) void end end def void $void ||= Void.new end (list_or_nil || void).each { |x| p x } Though, a core implementation of Void would be much more efficient b/c it would not need to even bother with method_missing, and just know to return void and move on. Hmmm... actually this has been discussed in another context before (Hash lookup) as Null. Looks like another good reason for it. T.