Range comparisons

  • Thread starter Yossef Mendelssohn
  • Start date
Y

Yossef Mendelssohn

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?
 
Y

Yossef Mendelssohn

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,308
Members
47,963
Latest member
NancyRyl51

Latest Threads

Top