[前][次][番号順一覧][スレッド一覧]

ruby-changes:51076

From: nobu <ko1@a...>
Date: Sat, 28 Apr 2018 16:31:38 +0900 (JST)
Subject: [ruby-changes:51076] nobu:r63283 (trunk): range.c: each on endless range

nobu	2018-04-28 16:31:32 +0900 (Sat, 28 Apr 2018)

  New Revision: 63283

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63283

  Log:
    range.c: each on endless range
    
    * range.c (range_each): endless range begins with string-like
      object should iterate from the converted result string, as well
      as `#each` on a string-end range or `#step` method on an endless
      range, i.e., `begin.succ` should not be called.

  Modified files:
    trunk/range.c
    trunk/test/ruby/test_range.rb
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 63282)
+++ test/ruby/test_range.rb	(revision 63283)
@@ -288,6 +288,19 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L288
     o = Object.new
     def o.to_int() 1 end
     assert_nothing_raised("[ruby-dev:34558]") { (0..2).step(o) {|x| } }
+
+    o = Object.new
+    class << o
+      def to_str() "a" end
+      def <=>(other) to_str <=> other end
+    end
+
+    a = []
+    (o.."c").step(1) {|x| a << x}
+    assert_equal(["a", "b", "c"], a)
+    a = []
+    (o..).step(1) {|x| a << x; break if a.size >= 3}
+    assert_equal(["a", "b", "c"], a)
   end
 
   def test_step_ruby_core_35753
@@ -348,6 +361,19 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L361
     a = []
     r2.each {|x| a << x }
     assert_equal([], a)
+
+    o = Object.new
+    class << o
+      def to_str() "a" end
+      def <=>(other) to_str <=> other end
+    end
+
+    a = []
+    (o.."c").each {|x| a << x}
+    assert_equal(["a", "b", "c"], a)
+    a = []
+    (o..).each {|x| a << x; break if a.size >= 3}
+    assert_equal(["a", "b", "c"], a)
   end
 
   def test_begin_end
Index: range.c
===================================================================
--- range.c	(revision 63282)
+++ range.c	(revision 63283)
@@ -834,10 +834,9 @@ range_each(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L834
 		args[1] = EXCL(range) ? Qtrue : Qfalse;
 		rb_block_call(tmp, rb_intern("upto"), 2, args, each_i, 0);
 	    }
-	    else if (RB_TYPE_P(beg, T_STRING)) {
-		rb_str_upto_endless_each(beg, (VALUE (*)(VALUE, VALUE))each_i, 0);
+	    else {
+		rb_str_upto_endless_each(tmp, (VALUE (*)(VALUE, VALUE))each_i, 0);
 	    }
-	    else goto inf_loop;
 	}
 	else {
 	    if (!discrete_object_p(beg)) {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]