8000 merge revision(s) da4464b824857d7610f9865ceb452ce0ead49164: [Backport… · github/ruby@469a0a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 469a0a4

Browse files
committed
merge revision(s) da4464b: [Backport #19426]
[Bug #19426] Fix endless `Range#step` with `#succ` method
1 parent 998c26c commit 469a0a4

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

range.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,11 @@ range_step(int argc, VALUE *argv, VALUE range)
532532
rb_raise(rb_eTypeError, "can't iterate from %s",
533533
rb_obj_classname(b));
534534
}
535-
range_each_func(range, step_i, (VALUE)iter);
535+
if (!NIL_P(e))
536+
range_each_func(range, step_i, (VALUE)iter);
537+
else
538+
for (;; b = rb_funcallv(b, id_succ, 0, 0))
539+
step_i(b, (VALUE)iter);
536540
}
537541
}
538542
return range;

test/ruby/test_range.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,26 @@ def test_step_ruby_core_35753
393393
assert_equal(4, (1.0...5.6).step(1.5).to_a.size)
394394
end
395395

396+
def test_step_with_succ
397+
c = Struct.new(:i) do
398+
def succ; self.class.new(i+1); end
399+
def <=>(other) i <=> other.i;end
400+
end.new(0)
401+
402+
result = []
403+
(c..c.succ).step(2) do |d|
404+
result << d.i
405+
end
406+
assert_equal([0], result)
407+
408+
result = []
409+
(c..).step(2) do |d|
410+
result << d.i
411+
break if d.i >= 4
412+
end
413+
assert_equal([0, 2, 4], result)
414+
end
415+
396416
def test_each
397417
a = []
398418
(0..10).each {|x| a << x }
@@ -457,6 +477,26 @@ def <=>(other) to_str <=> other end
457477
assert_equal(["a", "b", "c"], a)
458478
end
459479

480+
def test_each_with_succ
481+
c = Struct.new(:i) do
482+
def succ; self.class.new(i+1); end
483+
def <=>(other) i <=> other.i;end
484+
end.new(0)
485+
486+
result = []
487+
(c..c.succ).each do |d|
488+
result << d.i
489+
end
490+
assert_equal([0, 1], result)
491+
492+
result = []
493+
(c..).each do |d|
494+
result << d.i
495+
break if d.i >= 4
496+
end
497+
assert_equal([0, 1, 2, 3, 4], result)
498+
end
499+
460500
def test_begin_end
461501
assert_equal(0, (0..1).begin)
462502
assert_equal(1, (0..1).end)

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 7
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 261
14+
#define RUBY_PATCHLEVEL 262
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)
0