Class: SafeNil

Inherits:
BlankSlate show all
Includes:
Singleton
Defined in:
lib/quality_extensions/safe_nil.rb

Overview

Extending BasicObject because it provides us with a clean slate. It is similar to Object except it has almost all the standard methods stripped away so that we will hit method_missing for almost all method routing.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



63
64
65
66
# File 'lib/quality_extensions/safe_nil.rb', line 63

def method_missing(method, *args, &block)
  return nil  unless nil.respond_to?(method)      # A much faster alternative to 'rescue nil' that can be used most of the time to speed things up.
  nil.send(method, *args, &block) rescue nil
end

Instance Method Details

#inspectObject

undef inspect # Use nil’s version of inspect… although I wonder whether it would be better to have it return “SafeNil” so you know it’s different than a normal nil.



59
60
61
# File 'lib/quality_extensions/safe_nil.rb', line 59

def inspect
  'SafeNil'
end

#nil?Boolean

Returns:

  • (Boolean)


68
# File 'lib/quality_extensions/safe_nil.rb', line 68

def nil?; true; end