# Range comparisons

**URL:** <https://www.ruby-forum.com/t/range-comparisons/105922>\
**Category:** Ruby\
**Created:** [June 4, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/range-comparisons/105922 "2007-06-04T17:57:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yossef\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/a87d85/32.png) [@Yossef\_M](https://www.ruby-forum.com/u/Yossef_M)\
**Post date:** [June 4, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/range-comparisons/105922/1 "2007-06-04T17:57:01Z")

</div>

Something came up as I was testing some ideas. I know Range objects  
can indicate whether a specific object is an included element, but  
something I thought might work didn’t:

irb(main):001:0\> (1…10).include?(2…6)  
=\> false

I know a simple case like this can be covered with (1…10).include? 2  
and (1…10).include? 6 (range1 includes both range2.first and  
range2.last), but doing it that way seems not entirely right,  
especially when it comes to other cases. A big problem, as I see it,  
would be trying to figure out what to do if the second range excludes  
its end.

Maybe I’m just thinking about this completely wrong. Maybe I should  
be using Sets or something. Knowing about intersects would be nice,  
too.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![Yossef\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/a87d85/32.png) [@Yossef\_M](https://www.ruby-forum.com/u/Yossef_M)\
**Post date:** [June 4, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/range-comparisons/105922/2 "2007-06-04T18:11:04Z")

</div>

Here is an interesting discussion.

[http://opensoul.org/2007/2/13/ranges-include-or-overlap-with-ranges](http://opensoul.org/2007/2/13/ranges-include-or-overlap-with-ranges)

Anyway, nothing that would require more than 10 lines of code.

---

<div class="post-metadata">

**Author:** ![Yossef\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/a87d85/32.png) [@Yossef\_M](https://www.ruby-forum.com/u/Yossef_M)\
**Post date:** [June 4, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/range-comparisons/105922/3 "2007-06-04T18:21:05Z")

</div>

On 04.06.2007 17:56, Yossef M. wrote:

> especially when it comes to other cases. A big problem, as I see it,  
> would be trying to figure out what to do if the second range excludes  
> its end.
> 
> Maybe I’m just thinking about this completely wrong. Maybe I should  
> be using Sets or something. Knowing about intersects would be nice,  
> too.
> 
> Any thoughts?

Similar discussions have come up here before. The net was, that because  
of all sorts of issues (which you partly identified yourself already) it  
would not be wise to extend functionality of Range#include? beyond the  
current behavior. You’ll sure find more detail in the archives but one  
reason for example is that other Enumerable’s include? methods always  
check for single element membership only. For all the other operations  
there is - and & etc.

18:13:10 [contentreporter]: irb -r set  
irb(main):001:0\> s1=(1…10).to\_set  
=\> #\<Set: {5, 6, 1, 7, 2, 8, 3, 9, 4, 10}\>  
irb(main):002:0\> s2=(1…10).to\_set  
=\> #\<Set: {5, 6, 1, 7, 2, 8, 3, 9, 4}\>  
irb(main):003:0\> s1 == s2  
=\> false  
irb(main):004:0\> s1 - s2  
=\> #\<Set: {10}\>  
irb(main):005:0\> s2 - s1  
=\> #\<Set: {}\>  
irb(main):010:0\> s2.superset? s1  
=\> false  
irb(main):011:0\> s1.superset? s2  
=\> true

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Yossef\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/a87d85/32.png) [@Yossef\_M](https://www.ruby-forum.com/u/Yossef_M)\
**Post date:** [June 6, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/range-comparisons/105922/4 "2007-06-06T21:49:37Z")

</div>

Thanks to both of you (CHubas and Robert). I didn’t see the replies  
because either I don’t get replies to my threads e-mailed to me or I’m  
just oblivious.

I’ve written a few things myself, things on the order of  
Range#contains?, Range#contained\_by?, Range#overlaps?. The real  
trick, however comes when I’m using extensions built on top of Range,  
extensions that allow a range-ish objects that have open ends. I’m  
not talking about excluding the end, like 1…10, but allowing the  
absence of an end, like 1…nil. Now, that can be done easily enough  
with numbers and Infinity (1.0/0), but it gets troublesome when using,  
say, Times or Dates.
