class RSpec::Matchers::BuiltIn::BeBetween

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

Public Class Methods

new(min, max) click to toggle source
# File lib/rspec/matchers/built_in/be_between.rb, line 8
def initialize(min, max)
  @min, @max = min, max
  inclusive
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/be_between.rb, line 57
def description
  "be between #{description_of @min} and #{description_of @max} (#{@mode})"
end
exclusive() click to toggle source

@api public Makes the between comparison exclusive.

@example

expect(3).to be_between(2, 4).exclusive
# File lib/rspec/matchers/built_in/be_between.rb, line 33
def exclusive
  @less_than_operator = :<
  @greater_than_operator = :>
  @mode = :exclusive
  self
end
failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/be_between.rb, line 51
def failure_message
  "#{super}#{not_comparable_clause}"
end
inclusive() click to toggle source

@api public Makes the between comparison inclusive.

@example

expect(3).to be_between(2, 3).inclusive

@note The matcher is inclusive by default; this simply provides

a way to be more explicit about it.
# File lib/rspec/matchers/built_in/be_between.rb, line 21
def inclusive
  @less_than_operator = :<=
  @greater_than_operator = :>=
  @mode = :inclusive
  self
end
matches?(actual) click to toggle source

@api private @return [Boolean]

# File lib/rspec/matchers/built_in/be_between.rb, line 42
def matches?(actual)
  @actual = actual
  comparable? && compare
rescue ArgumentError
  false
end

Private Instance Methods

comparable?() click to toggle source
# File lib/rspec/matchers/built_in/be_between.rb, line 63
def comparable?
  @actual.respond_to?(@less_than_operator) && @actual.respond_to?(@greater_than_operator)
end
compare() click to toggle source
# File lib/rspec/matchers/built_in/be_between.rb, line 71
def compare
  @actual.__send__(@greater_than_operator, @min) && @actual.__send__(@less_than_operator, @max)
end
not_comparable_clause() click to toggle source
# File lib/rspec/matchers/built_in/be_between.rb, line 67
def not_comparable_clause
  ", but it does not respond to `#{@less_than_operator}` and `#{@greater_than_operator}`" unless comparable?
end