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

Instance Method Details

#case?(other) ⇒ true, false

Test whether ‘other` is a case of `self` via `#===` method.

Returns:

  • (true, false)

    Whether ‘other` is a case of `self`.



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.)

Returns:

  • (true, false)

    Whether ‘self` is equal to `other`.



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.

Returns:

  • (true, false)

    Whether ‘self` is `false`.



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.

Returns:

  • (true, false)

    Whether ‘self` is identical to `other`.



109
110
111
# File 'lib/bang.rb', line 109

def identical?(other)
  other.object_id == object_id
end

#like?(other) ⇒ true, false

TODO:

Should ‘like?` this include `=~` also?

Test whether ‘self` is like `other`. Like is broad equality measure testing `identical?`, `eql?`, `==` and `===`.

Returns:

  • (true, false)

    Whether ‘self` is like `other`.



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.

Returns:

  • (true, false)

    Whether ‘self` matches `other`.



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`.

Returns:

  • (true, false)

    True if block succeeds, otherwise 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`.

Returns:

  • (true, false)

    Whether ‘self` was thrown.



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.

Returns:

  • (true, false)

    Whether ‘self` is `true`.



168
169
170
# File 'lib/bang.rb', line 168

def true?
  TrueClass === self
end