I found out from heckle that
[1, 2, 3].each(&nil)
doesn't cause any errors - it just returns an enumerator.
By contrast,
[1, 2, 3].each(&"")
raises
TypeError: wrong argument type String (expected Proc)
Also, &nil
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
It's not because NilClass
implements to_proc
- I checked the RDoc.
I can understand why it'd be nice to have &nil
, but I'm not sure how it's done. Is this just one of the ways nil
has special behavior not shared by other objects?