From: Andrew Grimm Date: 2012-05-12T08:07:21+09:00 Subject: Re: Why do you think of NilClass#to_proc? On Sat, May 12, 2012 at 6:42 AM, Jan E. wrote: > Hi, > > Thomas Sawyer wrote in post #1060469: >> I have two minds about using a NilClass#to_proc >> >>     class NilClass >>       def to_proc >>         Proc.new{ |*a| a } >>       end >>     end >> >> Or >> >>     class NilClass >>       def to_proc >>         Proc.new{ nil } >>       end >>     end >> >> Which makes the most sense? > > The second solution seems more intuitive to me, since it's a kind of > "empty proc": all parameters are discarded and the proc returns nil. > > But like Bartosz already said: What's the use case for this feature? > I had a look at nil last year http://stackoverflow.com/questions/7410748/how-does-foonil-behave-differently-than-foonot-a-proc [1, 2, 3].each(&nil) doesn't cause any errors, it just returns an enumerator. Passing in &nil to a method causes block_given to return false: def block_given_tester if block_given? puts "Block given" else puts "Block not given" end end block_given_tester(&nil) # => Block not given Even when I create NilClass#to_proc, it still says block_given? is false, unless I explicitly call nil.to_proc: class NilClass def to_proc Proc.new{ nil } end end block_given_tester(&nil) # => Block not given block_given_tester(&nil.to_proc) # => Block given I'd try to avoid monkeypatching NilClass if possible. Andrew