Class: NilClass

Inherits:
Object show all
Defined in:
lib/quality_extensions/safe_nil.rb,
lib/quality_extensions/object/default.rb,
lib/quality_extensions/chainable_safe_nil.rb,
lib/quality_extensions/nil_method_missing.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

This allows you to call undefined methods on nil without an exception being raised.

This gives us a much conciser alternative to this common pattern:

might_be_nil ? might_be_nil.some_method : nil

or

(might_be_nil && might_be_nil.some_method)

… where might_be_nil is something that you hope is not nil most of the time, but which may on accosion be nil (and when it is, you don’t want an exception to be raised!).

For example, accessing a key from a hash:

(hash[:a] && hash[:a][:b] && hash[:a][:b].some_method)

With NilClass#method_missing, that simply becomes

hash[:a][:b].some_method)

The caveat with this approach is that it requires changing the behavior of a core class, NilClass, which could potentially have undesirable effects on code that expects the original behavior. Don’t require this file unless you are sure that you want all nils everywhere to have this behavior.

For a safer alternative that doesn’t require monkey-patching NilClass, consider using the _? method.



35
36
37
# File 'lib/quality_extensions/nil_method_missing.rb', line 35

def method_missing(*args)
  nil
end

Instance Method Details

#_?Boolean

The _? method just returns the object itself for all objects except for nil. For nil, _? will return a special version of nil (actually, an instance of SafeNil) that lets you call undefined methods.

If you call an undefined method on nil._?, you will get back nil – rather than raising an exception, which is what would happen if you called the same method on a plain old nil!

_? gives us a much conciser alternative to this common pattern:

might_be_nil ? might_be_nil.some_method : nil

or

(might_be_nil && might_be_nil.some_method)

… where might_be_nil is something that you hope is not nil most of the time, but which may on occasion be nil (and when it is, you don’t want an exception to be raised!).

For example, accessing a key from a hash:

(hash[:a] && hash[:a][:b] && hash[:a][:b].some_method)

With _?, that simply becomes

hash[:a]._?[:b]._?.some_method)

Returns:

  • (Boolean)


54
55
56
# File 'lib/quality_extensions/safe_nil.rb', line 54

def _?
  SafeNil.instance
end

#__?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/quality_extensions/chainable_safe_nil.rb', line 54

def __?
  ChainableSafeNil.instance
end

#default!(default_value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/quality_extensions/object/default.rb', line 22

def default!(default_value)
  #self.become default_value
  #self.replace default_value
  #self = default_value

  # Not sure how to implemnet this! ... without writing/using a C extension that lets me turn a NilClass object into another object.
end