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

ruby-changes:52054

From: mrkn <ko1@a...>
Date: Fri, 10 Aug 2018 13:49:50 +0900 (JST)
Subject: [ruby-changes:52054] mrkn:r64262 (trunk): enumerator.c: fix for non-integral argument for ArithmeticSequence#last

mrkn	2018-08-10 13:49:44 +0900 (Fri, 10 Aug 2018)

  New Revision: 64262

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

  Log:
    enumerator.c: fix for non-integral argument for ArithmeticSequence#last
    
    This fixes a bug of Enumerator::ArithmeticSequence#last in the case that
    a non-integral argument is passed.

  Modified files:
    trunk/enumerator.c
    trunk/test/ruby/test_arithmetic_sequence.rb
Index: test/ruby/test_arithmetic_sequence.rb
===================================================================
--- test/ruby/test_arithmetic_sequence.rb	(revision 64261)
+++ test/ruby/test_arithmetic_sequence.rb	(revision 64262)
@@ -195,6 +195,30 @@ class TestArithmeticSequence < Test::Uni https://github.com/ruby/ruby/blob/trunk/test/ruby/test_arithmetic_sequence.rb#L195
     assert_equal([2.0, 0.0, -2.0], seq.last(3))
   end
 
+  def test_last_with_float
+    res = (1..3).step(2).last(2.0)
+    assert_equal([1, 3], res)
+    assert_instance_of Integer, res[0]
+    assert_instance_of Integer, res[1]
+
+    res = (1..3).step(2).last(5.0)
+    assert_equal([1, 3], res)
+    assert_instance_of Integer, res[0]
+    assert_instance_of Integer, res[1]
+  end
+
+  def test_last_with_rational
+    res = (1..3).step(2).last(2r)
+    assert_equal([1, 3], res)
+    assert_instance_of Integer, res[0]
+    assert_instance_of Integer, res[1]
+
+    res = (1..3).step(2).last(10/2r)
+    assert_equal([1, 3], res)
+    assert_instance_of Integer, res[0]
+    assert_instance_of Integer, res[1]
+  end
+
   def test_to_a
     assert_equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1.step(10).to_a)
     assert_equal([1, 3, 5, 7, 9], 1.step(10, 2).to_a)
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 64261)
+++ enumerator.c	(revision 64262)
@@ -2495,6 +2495,9 @@ arith_seq_last(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2495
     }
 
     rb_scan_args(argc, argv, "1", &nv);
+    if (!RB_INTEGER_TYPE_P(nv)) {
+        nv = rb_to_int(nv);
+    }
     if (RTEST(rb_int_gt(nv, len))) {
         nv = len;
     }

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

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