On Wed, 2007-02-21 at 09:04 +0900, dblack / wobblini.net wrote:
> Hi --
> 
> On Wed, 21 Feb 2007, Daniel Schierbeck wrote:
> 
> > On Wed, 2007-02-21 at 08:00 +0900, Daniel Finnie wrote:
> >> I think your implementation is cleaner however the endpoints of a Range
> >> do not have to implement #succ.
> >>
> >> class Range
> >>    def span? other
> >>      include? other.first and
> >>        (other.exclude_end? ? other.last < last : other.last <= last )
> >>    end
> >> end
> >
> > I've tried that implementation, but i doesn't work if you consider this
> > valid:
> >
> >  (4..8).span? 6...9
> >
> > Since 6...9 yields the same values as 6..8. Or am I wrong to expect the
> > above?
> 
> I don't think ranges really yield values.  Some of them can be
> converted to arrays, but a range qua range is really just two
> endpoints, between which everything comparable to those endpoints
> either is or isn't.
> 
> I'm not sure I have my head around what span? is supposed to be doing.
> Can you write some test cases?  I keep thinking of this:
> 
>    include?(other.first) and include?(other.last)
> 
> but I don't think that's what you're trying to do with span?.

Here are my tests:

	class RangeSpanTest < Test::Unit::TestCase
	  def setup
	    @range = 4..8
	  end

	  def test_spans_subrange
	    assert @range.span?(5..7)
	  end

	  def test_not_spans_range
	    assert !@range.span?(9..14)
	  end

	  def test_spans_excluding_range
	    assert @range.span?(6...9)
	  end

	  def test_not_spans_superrange
	    assert !@range.span?(2..10)
	  end
	end

Daniel Finnie has a point about end-excluding ranges. But in that case,
wouldn't this suffice?

  def span? other
    include? other.first and include? other.last
  end

#overlap? should return true if the two ranges *overlap*, e.g.

   |-----|
        |-----|

span? should only return true if the receiver spans at least the
entirety of the argument, e.g.

   |--------------|
      |-------|


Cheers,
Daniel