1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

range.c: Add Range#%

[Feature #14697] [ruby-core:86588]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2018-09-28 02:18:58 +00:00
parent b7d6367979
commit 85f192b075
3 changed files with 20 additions and 0 deletions

4
NEWS
View file

@ -217,6 +217,10 @@ sufficient information, see the ChangeLog file or Redmine
* `Range`
* New methods:
* Added `Range#%` instance method. [Feature #14697]
* Incompatible changes:
* `Range#===` now uses `#cover?` instead of `#include?` method.

View file

@ -497,6 +497,12 @@ range_step(int argc, VALUE *argv, VALUE range)
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)
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);

View file

@ -322,6 +322,15 @@ class TestRange < Test::Unit::TestCase
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)