class RSpec::Matchers::BuiltIn::BeWithin

Not intended to be instantiated directly.
Provides the implementation for ‘be_within`.
@api private

def description

Returns:
  • (String) -

Other tags:
    Api: - private
def description
  "be within #{@delta}#{@unit} of #{expected_formatted}"
end

def failure_message

Returns:
  • (String) -

Other tags:
    Api: - private
def failure_message
  "expected #{actual_formatted} to #{description}#{not_numeric_clause}"
end

def failure_message_when_negated

Returns:
  • (String) -

Other tags:
    Api: - private
def failure_message_when_negated
  "expected #{actual_formatted} not to #{description}"
end

def initialize(delta)

def initialize(delta)
  @delta = delta
end

def matches?(actual)

Other tags:
    Private: -
def matches?(actual)
  @actual = actual
  raise needs_expected unless defined? @expected
  numeric? && (@actual - @expected).abs <= @tolerance
end

def needs_expected

def needs_expected
  ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
end

def not_numeric_clause

def not_numeric_clause
  ", but it could not be treated as a numeric value" unless numeric?
end

def numeric?

def numeric?
  @actual.respond_to?(:-)
end

def of(expected)

Other tags:
    Api: - public
def of(expected)
  @expected  = expected
  @tolerance = @delta
  @unit      = ''
  self
end

def percent_of(expected)

Other tags:
    Api: - public
def percent_of(expected)
  @expected  = expected
  @tolerance = @expected.abs * @delta / 100.0
  @unit      = '%'
  self
end