Class: Matchi::BeWithin::Of
- Inherits:
-
Object
- Object
- Matchi::BeWithin::Of
- Defined in:
- lib/matchi/be_within.rb
Overview
Nested class that performs the actual comparison.
This class implements the actual matching logic, comparing the provided value against the expected value using the specified delta.
Instance Method Summary collapse
-
#initialize(delta, expected) ⇒ Of
constructor
private
Initialize the matcher with a delta and an expected value.
-
#match? { ... } ⇒ Boolean
Checks if the yielded value is within the accepted range.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(delta, expected) ⇒ Of
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the matcher with a delta and an expected value.
103 104 105 106 107 108 109 110 |
# File 'lib/matchi/be_within.rb', line 103 def initialize(delta, expected) raise ::ArgumentError, "delta must be a Numeric" unless delta.is_a?(::Numeric) raise ::ArgumentError, "expected must be a Numeric" unless expected.is_a?(::Numeric) raise ::ArgumentError, "delta must be non-negative" if delta.negative? @delta = delta @expected = expected end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the yielded value is within the accepted range.
The value is considered within range if its absolute difference from the expected value is less than or equal to the specified delta.
130 131 132 133 134 |
# File 'lib/matchi/be_within.rb', line 130 def match? raise ::ArgumentError, "a block must be provided" unless block_given? (@expected - yield).abs <= @delta end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
144 145 146 |
# File 'lib/matchi/be_within.rb', line 144 def to_s "be within #{@delta} of #{@expected}" end |