Skip to content

Commit

Permalink
Workaround for Range#relative_to
Browse files Browse the repository at this point in the history
This is a workaround until https://bugs.ruby-lang.org/issues/6203
is addressed and we can apply the fix from there. Or this will be
a permanent work around in the unlikely case that the change is
rejected.
  • Loading branch information
ferrous26 committed Mar 31, 2012
1 parent fc235b1 commit 4defae5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions range.c
Expand Up @@ -969,6 +969,8 @@ range_relative_to(VALUE range, SEL sel, VALUE max)
{
long beg, len, m = NUM2LONG(max);
rb_range_beg_len(range, &beg, &len, m, 0);
if (beg + len - 1 > m)
len -= 1;
return rb_range_new(LONG2NUM(beg), LONG2NUM(beg + len - 1), 0);
}

Expand Down
6 changes: 3 additions & 3 deletions spec/macruby/core/range_spec.rb
Expand Up @@ -11,9 +11,8 @@
it "responds to #relative_to, which returns a new Range object without negative indicies" do
( 0 .. 10).relative_to(11).should == (0..10)
( 1 .. 9 ).relative_to(11).should == (1..9 )
##
# TODO: This spec fails right now, though it is the intended behaviour.
# ( 0 .. 15).relative_to(11).should == (0..10)
( 0 .. 15).relative_to(11).should == (0..10)
( 0 .. 11).relative_to(11).should == (0..10)

( 0 .. -1 ).relative_to(11).should == (0..10)
( 0 .. -2 ).relative_to(11).should == (0..9 )
Expand All @@ -24,6 +23,7 @@
(-10.. -5 ).relative_to(11).should == (1..6 )

( 4 ... 11).relative_to(11).should == (4..10)
( 4 ... 15).relative_to(11).should == (4..10)
( 4 ... 10).relative_to(11).should == (4..9 )
( 4 ...-1 ).relative_to(11).should == (4..9 )
(-11...-1 ).relative_to(11).should == (0..9 )
Expand Down

0 comments on commit 4defae5

Please sign in to comment.