Module: Tins::Null
Overview
Tins::Null provides an implementation of the null object pattern in Ruby.
The null object pattern is a behavioral design pattern that allows you to avoid null references by providing a default object that implements the expected interface but does nothing. This eliminates the need for null checks throughout your codebase.
This module provides the core functionality for null objects, including:
-
Method missing behavior that returns self
-
Type conversion methods that return appropriate default values
-
Debugging support through NullPlus
Defined Under Namespace
Modules: Kernel
Instance Method Summary collapse
-
#as_json ⇒ nil
Convert to JSON (for compatibility with JSON serialization).
-
#blank? ⇒ Boolean
Check if object is blank.
-
#const_missing ⇒ self
Handle missing constants by returning self.
-
#inspect ⇒ String
Inspect representation.
-
#method_missing ⇒ self
Handle missing methods by returning self, allowing method chaining.
-
#nil? ⇒ Boolean
Check if object is nil.
-
#to_a ⇒ Array
Convert to array.
-
#to_f ⇒ Float
Convert to float.
-
#to_i ⇒ Integer
Convert to integer.
-
#to_json ⇒ String
Convert to JSON string.
-
#to_s ⇒ String
Convert to string representation.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ self
Handle missing methods by returning self, allowing method chaining.
34 35 36 |
# File 'lib/tins/null.rb', line 34 def method_missing(*) self end |
Instance Method Details
#as_json ⇒ nil
Convert to JSON (for compatibility with JSON serialization).
97 98 99 |
# File 'lib/tins/null.rb', line 97 def as_json(*) nil end |
#blank? ⇒ Boolean
Check if object is blank.
90 91 92 |
# File 'lib/tins/null.rb', line 90 def blank? true end |
#const_missing ⇒ self
Handle missing constants by returning self.
41 42 43 |
# File 'lib/tins/null.rb', line 41 def const_missing(*) self end |
#inspect ⇒ String
Inspect representation.
76 77 78 |
# File 'lib/tins/null.rb', line 76 def inspect 'NULL' end |
#nil? ⇒ Boolean
Check if object is nil.
83 84 85 |
# File 'lib/tins/null.rb', line 83 def nil? true end |
#to_f ⇒ Float
Convert to float.
55 56 57 |
# File 'lib/tins/null.rb', line 55 def to_f 0.0 end |
#to_i ⇒ Integer
Convert to integer.
62 63 64 |
# File 'lib/tins/null.rb', line 62 def to_i 0 end |
#to_json ⇒ String
Convert to JSON string.
104 105 106 |
# File 'lib/tins/null.rb', line 104 def to_json(*) 'null' end |
#to_s ⇒ String
Convert to string representation.
48 49 50 |
# File 'lib/tins/null.rb', line 48 def to_s '' end |