Class: SafeNil
- 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
-
#inspect ⇒ Object
undef inspect # Use nil’s version of inspect…
- #method_missing(method, *args, &block) ⇒ Object
- #nil? ⇒ Boolean
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
#inspect ⇒ Object
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
68 |
# File 'lib/quality_extensions/safe_nil.rb', line 68 def nil?; true; end |