Class: NilClass

Inherits:
Object show all
Defined in:
lib/active_support/whiny_nil.rb

Overview

Extensions to nil which allow for more helpful error messages for people who are new to rails.

The aim is to ensure that when users pass nil to methods where that isn’t appropriate, instead of NoMethodError and the name of some method used by the framework users will see a message explaining what type of object was expected.

Constant Summary collapse

WHINERS =
[ ::ActiveRecord::Base, ::Array ]
NIL_WARNING_MESSAGE =
<<-end_message unless const_defined?(:NIL_WARNING_MESSAGE)
WARNING:  You have a nil object when you probably didn't expect it!  Odds are you
want an instance of %s instead.

Look in the callstack to see where you're working with an object that could be nil.
Investigate your methods and make sure th
@@method_class_map =
Hash.new

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



26
27
28
29
30
31
32
# File 'lib/active_support/whiny_nil.rb', line 26

def method_missing(method, *args, &block)
  if @@method_class_map.include?(method)
    raise_nil_warning_for @@method_class_map[method], caller
  else
    super
  end
end

Instance Method Details

#idObject

Raises:

  • (RuntimeError)


21
22
23
# File 'lib/active_support/whiny_nil.rb', line 21

def id
  raise RuntimeError, "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id", caller
end