> class Range
>     alias_method :old_include?, :include?
>
>     def include?(obj)
>         if obj.is_a? Range
>             old_include? obj.first and
>             old_include? obj.exclude_end? ? obj.last-1 : obj.last
                                              ^^^^^^^^^^
Doesn't work for ranges like 'a'..'z'. If I understand well, the only
requirement for a Range is that it's elements have <=> and succ as
operations.
>         else
>             old_include? obj
>         end
>     end
>
>     def intersect?(aRange)
>         aRange.each do |i|
>             return true if include? i
>         end
>
>         false
>     end
> end

My solution is below. The include? should be slightly faster since it
calls <=> only twice. The intersect? is also faster for large ranges. Also
it works for ranges where first is larger than last (the program above
says false to (("e"..."e").include? ("e"..."e"))  while I think it should
say true (all elements of "e"..."e" are in "e"..."e", since there are
none). But the original Range isn't always consistent there either since
("a"..."a").each do |i| p i end prints "a" (replace "a" with 0 and it
prints nothing), but ("a"..."a").include? "a" says false. Anyway, the code
below is probably harder to find bugs in...

Peter


class Range

  alias_method :old_include?, :include?

  def include?(obj)
    if obj.is_a? Range
      lb1 = first.succ
      ub1 = exclude_end? ? last : last.succ
      lb2 = obj.first.succ
      ub2 = obj.exclude_end? ? obj.last : obj.last.succ
      (((lb2 <=> lb1) >= 0) && ((ub1 <=> ub2) >= 0))
    else
      old_include? obj
    end
  end

  def intersect?(obj)
    lb1 = first.succ
    ub1 = exclude_end? ? last : last.succ
    lb2 = obj.first.succ
    ub2 = obj.exclude_end? ? obj.last : obj.last.succ
    if (lb2 <=> ub2) > 0 then
      false
    elsif (lb1 <=> ub1) > 0
      false
    else
      (((ub1 <=> lb2) >= 0) && ((ub2 <=> lb1) >= 0))
    end
  end
end

p (("a".."e").include? ("a".."e")) # true
p (("a".."e").include? ("a".."f")) # false
p (("a".."e").include? ("a"..."f")) # true
p (("a"..."e").include? ("a"..."f")) # false
p (("a"..."f").include? ("a"..."f")) # true
p (("e"..."e").include? ("e".."e")) # false
p (("e"..."e").include? ("e"..."e")) # true

p (("a".."e").intersect? ("e".."i")) # true
p (("a"..."e").intersect? ("e".."i")) # false
p (("a"..."e").intersect? ("d".."i")) # true
p (("a".."e").intersect? ("e"..."e")) # false