Class: SafeNil

Inherits:
BasicObject
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.

See NilClass#_?/Object#_?

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

See NilClass#_?/Object#_?



72
73
74
75
# File 'lib/quality_extensions/safe_nil.rb', line 72

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

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?; true; end

#nil?Boolean

See NilClass#_?/Object#_?

Returns:

  • (Boolean)


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

def nil?; true; end