module Bang::ObjectMixin

Mixin for Object class that adds some very useful query methods.

Public Instance Methods

case?(other) click to toggle source

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

@return [true,false] Whether ‘other` is a case of `self`.

# File lib/bang.rb, line 150
def case?(other)
  other === self
end
equal_to?(other) click to toggle source

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

@return [true,false] Whether ‘self` is equal to `other`.

# File lib/bang.rb, line 120
def equal_to?(other)
  other == self
end
Also aliased as: equals?
equals?(other)

Until we can use ‘#equal?`, lets make the best of it and offer the plural form as well.

Alias for: equal_to?
false?() click to toggle source

Test whether ‘self` is the `false` instance.

@return [true,false] Whether ‘self` is `false`.

# File lib/bang.rb, line 177
def false?
  FalseClass === self
end
identical?(other) click to toggle source

Is ‘self` identical with `other`? In other words, do two variables reference the one and the same object.

@return [true,false] Whether ‘self` is identical to `other`.

# File lib/bang.rb, line 109
def identical?(other)
  other.object_id == object_id
end
like?(other) click to toggle source

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

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

@return [true,false] Whether ‘self` is like `other`.

# File lib/bang.rb, line 138
def like?(other)
  other.identical?(self) ||
  other.eql?(self)       ||
  other.==(self)         ||
  other.===(self)
end
match?(other) click to toggle source

Test whether ‘self` matches `other` via `#=~` method.

@return [true,false] Whether ‘self` matches `other`.

# File lib/bang.rb, line 159
def match?(other)
  other =~ self
end
satisfy?(&block) click to toggle source

Yield block and return true if it runs without exception and does not return ‘nil` or `false`.

@return [true,false] True if block succeeds, otherwise false.

# File lib/bang.rb, line 208
def satisfy?(&block)
  begin
    block.call(self)
  rescue
    false
  end
end
thrown?() { || ... } click to toggle source

Yield the given block and return ‘true` if the `self` is throw, otherwise `false`.

@return [true,false] Whether ‘self` was thrown.

# 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?() click to toggle source

Test whether ‘self` is the `true` instance.

@return [true,false] Whether ‘self` is `true`.

# File lib/bang.rb, line 168
def true?
  TrueClass === self
end