Class: Hexx::Null

Inherits:
Object
  • Object
show all
Extended by:
Comparable
Defined in:
lib/hexx/null.rb

Overview

Note:

In Ruby the only objects that are false are nil and false. Use the double bang before any object that may be the Null.

The Null object class.

Except for some special cases (see examples below) calling any method returns the Null itself.

Examples:

An arbitrary method returns the Null itself.

Null.is_an_eagle_owl?  # => Null
Null.is_an_elk?        # => Null
Null.fly               # => Null
# ... etc.

An arbitrary method called with a block yields the block.

Null.when_fishing { p "I've got it!" }
# => "I've got it!"
# => Null

The Null object constructs itself.

Null.new
# => Null

The Null is nil.

Null.nil? # => true

The Null is equal to nil.

Null.eq? nil  # => true
Null.eq? Null # => true

The Null differs from non-nil

Null.eq? 0     # => false
Null.eq? false # => false

The Null is less than non-nil object:

Null < 0     # => true
Null < ""    # => true
Null < false # => true

The Null is falseу.

Null.false?  # => true

The Null isn't false in logical operations

false || Null # => Null

The Null isn't truthy.

Null.true?  # => false

The Null is truthy in logical operations.

true && Null # => Null

The Null is convertible to true.

!Null  # => true

Use a double bang before objects that may be the Null.

Null && true   # => true
!!Null && true # => false

The Null is convertible to the empty array.

Null.to_a # => []

The Null is convertible to the empty string.

Null.to_s # => ""

The Null is convertible to zero as an integer.

Null.to_i # => 0

The Null is convertible to zero as a float.

Null.to_f # => 0.0

The Null is convertible to zero as a complex.

Null.to_c # => (0+0i)

The Null is convertible to zero as a rational.

Null.to_c # => (0/1)

The Null is convertible to nil.

Null.to_nil # => nil

Class Method Summary collapse

Class Method Details

.!Boolean

Note:

In Ruby the only objects that are false are nil and false. Use the double bang before any object that may be the Null.

Converts nil to true.

Examples:

The Null is convertible to true.

!Null  # => true

Use a double bang before objects that may be the Null.

Null && true   # => true
!!Null && true # => false

107
108
109
# File 'lib/hexx/null.rb', line 107

def !
  true
end

.<=>(other) ⇒ -1, 0

Compares the Null to other object.

Examples:

The Null is less than non-nil object:

Null < 0     # => true
Null < ""    # => true
Null < false # => true

67
68
69
# File 'lib/hexx/null.rb', line 67

def <=>(other)
  other.nil? ? 0 : -1
end

.^(other) ⇒ false, true


187
188
189
# File 'lib/hexx/null.rb', line 187

def ^(other)
  ![self, nil, false].include? other
end

.eq?(other) ⇒ Boolean

Checks if Null is equal to other object.

Examples:

The Null is equal to nil.

Null.eq? nil  # => true
Null.eq? Null # => true

The Null differs from non-nil

Null.eq? 0     # => false
Null.eq? false # => false

54
55
56
# File 'lib/hexx/null.rb', line 54

def eq?(other)
  other.nil?
end

.false?Boolean

Checks if the Null is falseу.

Examples:

The Null is falseу.

Null.false?  # => true

The Null isn't false in logical operations

false || Null # => Null

90
91
92
# File 'lib/hexx/null.rb', line 90

def false?
  true
end

.newClass

The constructor constructs itself.

Examples:

The Null object constructs itself.

Null.new
# => Null

38
39
40
# File 'lib/hexx/null.rb', line 38

def new
  self
end

.nil?Boolean

Checks if the Null is nil.

Examples:

The Null is nil.

Null.nil? # => true

77
78
79
# File 'lib/hexx/null.rb', line 77

def nil?
  true
end

.respond_to?Boolean

Checks if a method defined.

Examples:

The Null responds to any object.

Null.respond_to? arbitrary_method
# => true

181
182
183
# File 'lib/hexx/null.rb', line 181

def respond_to?(*)
  true
end

.to_aArray

Converts the Null to the empty array.

Examples:

The Null is convertible to the empty array.

Null.to_a # => []

# File 'lib/hexx/null.rb', line 144


.to_cComplex

Converts the Null to zero as a complex.

Examples:

The Null is convertible to zero as a complex.

Null.to_c # => (0+0i)

# File 'lib/hexx/null.rb', line 162


.to_fFloat

Converts the Null to zero as a float.

Examples:

The Null is convertible to zero as a float.

Null.to_f # => 0.0

# File 'lib/hexx/null.rb', line 156


.to_iInteger

Converts the Null to zero as an integer.

Examples:

The Null is convertible to zero as an integer.

Null.to_i # => 0

# File 'lib/hexx/null.rb', line 150


.to_nilNilClass

Converts the Null to nil.

Examples:

The Null is convertible to nil.

Null.to_nil # => nil

140
141
142
# File 'lib/hexx/null.rb', line 140

def to_nil
  nil
end

.to_rRational

Converts the Null to zero as a rational.

Examples:

The Null is convertible to zero as a rational.

Null.to_c # => (0/1)

# File 'lib/hexx/null.rb', line 168


.to_sString

Converts the Null to the empty string.

Examples:

The Null is convertible to the empty string.

Null.to_s # => ""

130
131
132
# File 'lib/hexx/null.rb', line 130

def to_s
  ""
end

.true?Boolean

Checks if the Null is truthy.

Examples:

The Null isn't truthy.

Null.true?  # => false

The Null is truthy in logical operations.

true && Null # => Null

120
121
122
# File 'lib/hexx/null.rb', line 120

def true?
  false
end

.|(other) ⇒ false, true


193
194
195
# File 'lib/hexx/null.rb', line 193

def |(other)
  self.^(other)
end