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

ruby-changes:55226

From: mrkn <ko1@a...>
Date: Thu, 4 Apr 2019 12:34:57 +0900 (JST)
Subject: [ruby-changes:55226] mrkn:r67433 (trunk): range.c: support to make beginless arithmetic sequences

mrkn	2019-04-04 12:34:52 +0900 (Thu, 04 Apr 2019)

  New Revision: 67433

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

  Log:
    range.c: support to make beginless arithmetic sequences
    
    * range.c (range_step): fix the guard condition so that a beginless
      range can be turned into a beginless arithmetic sequence.
    
    * test/ruby/test_range.rb (test_step): add assertions for the above
      change.

  Modified files:
    trunk/range.c
    trunk/test/ruby/test_range.rb
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 67432)
+++ test/ruby/test_range.rb	(revision 67433)
@@ -226,6 +226,8 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L226
     assert_kind_of(Enumerator::ArithmeticSequence, (0..10).step(2))
     assert_kind_of(Enumerator::ArithmeticSequence, (0..10).step(0.5))
     assert_kind_of(Enumerator::ArithmeticSequence, (10..0).step(-1))
+    assert_kind_of(Enumerator::ArithmeticSequence, (..10).step(2))
+    assert_kind_of(Enumerator::ArithmeticSequence, (1..).step(2))
 
     assert_raise(ArgumentError) { (0..10).step(0) { } }
     assert_raise(ArgumentError) { (0..).step(-1) { } }
Index: range.c
===================================================================
--- range.c	(revision 67432)
+++ range.c	(revision 67433)
@@ -397,7 +397,9 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L397
     step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]);
 
     if (!rb_block_given_p()) {
-        if (rb_obj_is_kind_of(b, rb_cNumeric) && (NIL_P(e) || rb_obj_is_kind_of(e, rb_cNumeric))) {
+        const VALUE b_num_p = rb_obj_is_kind_of(b, rb_cNumeric);
+        const VALUE e_num_p = rb_obj_is_kind_of(e, rb_cNumeric);
+        if ((b_num_p && (NIL_P(e) || e_num_p)) || (NIL_P(b) && e_num_p)) {
             return rb_arith_seq_new(range, ID2SYM(rb_frame_this_func()), argc, argv,
                     range_step_size, b, e, step, EXCL(range));
         }

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

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