Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Ruby Range comparison implemented incorrectly?

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 are considered different? I don't think that's how it works in math though.

In PostgreSQL, it is implement correctly:

test=# select int4range(1,2, '[]') = int4range(1,3, '[)');
 ?column? 
----------
 t
(1 row)
like image 271
Dmytrii Nagirniak Avatar asked Dec 08 '22 07:12

Dmytrii Nagirniak


1 Answers

These ranges aren't equal - consider the case when you call include? with a floating-point value:

(1 .. 2).include? 2.5
false

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

They happen to return the same results if you compare them to integers, but that doesn't mean they're identical.

like image 155
Frank Schmitt Avatar answered Dec 11 '22 10:12

Frank Schmitt