Module: Bang::ObjectMixin
- Included in:
- Object
- Defined in:
- lib/bang.rb
Overview
Mixin for Object class that adds some very useful query methods.
Instance Method Summary collapse
-
#case?(other) ⇒ true, false
Test whether ‘other` is a case of `self` via `#===` method.
-
#equal_to?(other) ⇒ true, false
(also: #equals?)
Query method for ‘#==`.
-
#false? ⇒ true, false
Test whether ‘self` is the `false` instance.
-
#identical?(other) ⇒ true, false
Is ‘self` identical with `other`? In other words, do two variables reference the one and the same object.
-
#like?(other) ⇒ true, false
Test whether ‘self` is like `other`.
-
#match?(other) ⇒ true, false
Test whether ‘self` matches `other` via `#=~` method.
-
#satisfy?(&block) ⇒ true, false
Yield block and return true if it runs without exception and does not return ‘nil` or `false`.
-
#thrown?(&block) ⇒ true, false
Yield the given block and return ‘true` if the `self` is throw, otherwise `false`.
-
#true? ⇒ true, false
Test whether ‘self` is the `true` instance.
Instance Method Details
#case?(other) ⇒ true, false
Test whether ‘other` is a case of `self` via `#===` method.
150 151 152 |
# File 'lib/bang.rb', line 150 def case?(other) other === self end |
#equal_to?(other) ⇒ true, false Also known as: equals?
Query method for ‘#==`. We have to use the `_to` suffix becuase Ruby already defines the prepositionless term as a synonym for `#identical?`. (Hopefully that will change one day.)
120 121 122 |
# File 'lib/bang.rb', line 120 def equal_to?(other) other == self end |
#false? ⇒ true, false
Test whether ‘self` is the `false` instance.
177 178 179 |
# File 'lib/bang.rb', line 177 def false? FalseClass === self end |
#identical?(other) ⇒ true, false
Is ‘self` identical with `other`? In other words, do two variables reference the one and the same object.
109 110 111 |
# File 'lib/bang.rb', line 109 def identical?(other) other.object_id == object_id end |
#like?(other) ⇒ true, false
Should ‘like?` this include `=~` also?
Test whether ‘self` is like `other`. Like is broad equality measure testing `identical?`, `eql?`, `==` and `===`.
138 139 140 141 142 143 |
# File 'lib/bang.rb', line 138 def like?(other) other.identical?(self) || other.eql?(self) || other.==(self) || other.===(self) end |
#match?(other) ⇒ true, false
Test whether ‘self` matches `other` via `#=~` method.
159 160 161 |
# File 'lib/bang.rb', line 159 def match?(other) other =~ self end |
#satisfy?(&block) ⇒ true, false
Yield block and return true if it runs without exception and does not return ‘nil` or `false`.
208 209 210 211 212 213 214 |
# File 'lib/bang.rb', line 208 def satisfy?(&block) begin block.call(self) rescue false end end |
#thrown?(&block) ⇒ true, false
Yield the given block and return ‘true` if the `self` is throw, otherwise `false`.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/bang.rb', line 187 def thrown?(&block) pass = true catch(self) do begin yield rescue ArgumentError => err # 1.9 exception #msg += ", not #{err.message.split(/ /).last}" rescue NameError => err # 1.8 exception #msg += ", not #{err.name.inspect}" end pass = false end pass end |
#true? ⇒ true, false
Test whether ‘self` is the `true` instance.
168 169 170 |
# File 'lib/bang.rb', line 168 def true? TrueClass === self end |