class Matchi::BeWithin::Of

*BeWithin of* matcher.

Attributes

expected[R]

@return [Numeric] An expected value.

Public Class Methods

new(delta, expected) click to toggle source

Initialize the matcher with a delta and an expected value.

@example

require "matchi/be_within/of"

Matchi::BeWithin::Of.new(1, 41)

@param delta [Numeric] The accepted variation of the actual value. @param expected [Numeric] The expected value.

# File lib/matchi/be_within/of.rb, line 19
def initialize(delta, expected)
  @delta    = delta
  @expected = expected
end

Public Instance Methods

inspect() click to toggle source

A string containing a human-readable representation of the matcher.

# File lib/matchi/be_within/of.rb, line 43
def inspect
  "#{self.class}(#{@delta}, #{expected})"
end
matches?() { || ... } click to toggle source

Boolean comparison on the expected be_within by comparing the actual value and the expected value.

@example

require "matchi/be_within/of"

matcher = Matchi::BeWithin::Of.new(1, 41)

matcher.expected        # => 41
matcher.matches? { 42 } # => true

@yieldreturn [Numeric] The block of code to execute.

@return [Boolean] Comparison between the actual and the expected values.

# File lib/matchi/be_within/of.rb, line 38
def matches?
  (expected - yield).abs <= @delta
end
to_s() click to toggle source

Returns a string representing the matcher.

# File lib/matchi/be_within/of.rb, line 48
def to_s
  "be within #{@delta} of #{expected}"
end