refactor Range#include? to handle ranges with floats

This commit is contained in:
Sergey Nartimov 2011-12-29 10:50:15 +03:00
parent afd9512c0b
commit 952e9d9005
2 changed files with 6 additions and 1 deletions

View File

@ -9,7 +9,8 @@ class Range
# (5..9).include?(11) # => false # (5..9).include?(11) # => false
def include_with_range?(value) def include_with_range?(value)
if value.is_a?(::Range) if value.is_a?(::Range)
min <= value.min && max >= value.max operator = exclude_end? && !value.exclude_end? ? :< : :<=
include_without_range?(value.first) && value.last.send(operator, last)
else else
include_without_range?(value) include_without_range?(value)
end end

View File

@ -53,6 +53,10 @@ class RangeTest < Test::Unit::TestCase
assert !(2..8).include?(5..9) assert !(2..8).include?(5..9)
end end
def test_should_include_identical_exclusive_with_floats
assert (1.0...10.0).include?(1.0...10.0)
end
def test_blockless_step def test_blockless_step
assert_equal [1,3,5,7,9], (1..10).step(2) assert_equal [1,3,5,7,9], (1..10).step(2)
end end