Skip to content

Instantly share code, notes, and snippets.

@mcamiano
Created April 16, 2012 14:04
Show Gist options
  • Save mcamiano/2399031 to your computer and use it in GitHub Desktop.
Save mcamiano/2399031 to your computer and use it in GitHub Desktop.
Enumerable nil
$ rails c
nLoading development environment (Rails 3.2.1)
1.9.3p0 :001 > nil
=> nil
1.9.3p0 :002 > nil.nil?
=> true
1.9.3p0 :004 > nil.each
NoMethodError: undefined method `each' for nil:NilClass
from (irb):4
from /Users/mamiano/.rvm/gems/ruby-1.9.3-p0@checkie/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/mamiano/.rvm/gems/ruby-1.9.3-p0@checkie/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/mamiano/.rvm/gems/ruby-1.9.3-p0@checkie/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3p0 :006 > def nil.each
1.9.3p0 :007?> end
=> nil
1.9.3p0 :008 > nil.each { |f| puts f}
=> nil
1.9.3p0 :009 > class NilClass
1.9.3p0 :010?> include Enumerable
1.9.3p0 :011?> def each ; end
1.9.3p0 :012?> def to_a; []; end
1.9.3p0 :013?> end
=> nil
1.9.3p0 :014 > nil.each
=> nil
1.9.3p0 :015 > nil.to_a
=> []
1.9.3p0 :016 > nil.map
=> #<Enumerator: nil:map>
1.9.3p0 :017 > nil.map { |n| n }
=> []
1.9.3p0 :018 > nil.any? { |n| n.nil? }
=> false
1.9.3p0 :019 > quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment