ruby-changes:52657
From: mrkn <ko1@a...>
Date: Fri, 28 Sep 2018 11:19:03 +0900 (JST)
Subject: [ruby-changes:52657] mrkn:r64869 (trunk): range.c: Add Range#%
mrkn 2018-09-28 11:18:58 +0900 (Fri, 28 Sep 2018) New Revision: 64869 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64869 Log: range.c: Add Range#% [Feature #14697] [ruby-core:86588] Modified files: trunk/NEWS trunk/range.c trunk/test/ruby/test_range.rb Index: range.c =================================================================== --- range.c (revision 64868) +++ range.c (revision 64869) @@ -497,6 +497,12 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L497 return range; } +static VALUE +range_percent_step(VALUE range, VALUE step) +{ + return range_step(1, &step, range); +} + #if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T) union int64_double { int64_t i; @@ -1549,6 +1555,7 @@ Init_Range(void) https://github.com/ruby/ruby/blob/trunk/range.c#L1555 rb_define_method(rb_cRange, "hash", range_hash, 0); rb_define_method(rb_cRange, "each", range_each, 0); rb_define_method(rb_cRange, "step", range_step, -1); + rb_define_method(rb_cRange, "%", range_percent_step, 1); rb_define_method(rb_cRange, "bsearch", range_bsearch, 0); rb_define_method(rb_cRange, "begin", range_begin, 0); rb_define_method(rb_cRange, "end", range_end, 0); Index: NEWS =================================================================== --- NEWS (revision 64868) +++ NEWS (revision 64869) @@ -217,6 +217,10 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L217 * `Range` + * New methods: + + * Added `Range#%` instance method. [Feature #14697] + * Incompatible changes: * `Range#===` now uses `#cover?` instead of `#include?` method. Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 64868) +++ test/ruby/test_range.rb (revision 64869) @@ -322,6 +322,15 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L322 assert_equal(["a", "b", "c"], a) end + def test_percent_step + aseq = (1..10) % 2 + assert_equal(Enumerator::ArithmeticSequence, aseq.class) + assert_equal(1, aseq.begin) + assert_equal(10, aseq.end) + assert_equal(2, aseq.step) + assert_equal([1, 3, 5, 7, 9], aseq.to_a) + end + def test_step_ruby_core_35753 assert_equal(6, (1...6.3).step.to_a.size) assert_equal(5, (1.1...6).step.to_a.size) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/