Is the comparison of Ruby Range levels wrong?

As in the documentation , two ranges that represent the same elements are considered different:

(1..2).to_a # => [1, 2] 
(1...3).to_a # => [1, 2] 
(1..2) == (1...3) # => false 

Why are two ranges that represent the same elements considered different? I don't think this works in math.

In PostgreSQL, it runs correctly:

test=# select int4range(1,2, '[]') = int4range(1,3, '[)');
 ?column? 
----------
 t
(1 row)
+4
source share
2 answers

These ranges are not equal - consider the case when you call include?with a floating point:

(1 .. 2).include? 2.5
false

(1 ... 3).include? 2.5
true

They return to the same results if we compare them with integers, but this does not mean that they are identical.

+11
source

, to_a. , .

(1..2).cover?(2.99)
# => false

(1...3).cover?(2.99)
# => true
+3

Source: https://habr.com/ru/post/1547154/


All Articles