Class: Matchi::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/be_within.rb,
lib/matchi/be_within/of.rb

Overview

Builder for the be within DELTA of EXPECTED matcher. It is not a matcher itself: it does not respond to match?, so a framework that checks the matcher interface rejects a forgotten of.

Examples:

Matchi::BeWithin.new(0.5).of(3).match? { 3.2 } # => true

Defined Under Namespace

Classes: Of

Instance Method Summary collapse

Constructor Details

#initialize(delta) ⇒ BeWithin

Returns a new instance of BeWithin.

Parameters:

  • delta (Numeric) —

    the tolerance: a finite, real, non-negative number

Raises:

  • (ArgumentError) —

    when delta is not such a number



16
17
18
# File 'lib/matchi/be_within.rb', line 16

def initialize(delta)
  @delta = Number.non_negative!(delta, "delta")
end

Instance Method Details

#of(expected) ⇒ Matchi::BeWithin::Of

Returns the matcher.

Parameters:

  • expected (Numeric) —

    the center of the interval, a finite real number

Returns:

Raises:

  • (ArgumentError) —

    when expected is not a finite real Numeric



23
24
25
# File 'lib/matchi/be_within.rb', line 23

def of(expected)
  Of.new(@delta, expected)
end

#to_s ⇒ String

Returns:

  • (String)


28
29
30
# File 'lib/matchi/be_within.rb', line 28

def to_s
  "be within #{@delta.inspect}"
end