Method: Range#%
- Defined in:
- range.c
#%(n) {|element| ... } ⇒ self #%(n) ⇒ Object
Same as #step (but doesn’t provide default value for n
). The method is convenient for experssive producing of Enumerator::ArithmeticSequence.
array = [0, 1, 2, 3, 4, 5, 6]
# slice each second element:
seq = (0..) % 2 #=> ((0..).%(2))
array[seq] #=> [0, 2, 4, 6]
# or just
array[(0..) % 2] #=> [0, 2, 4, 6]
Note that due to operator precedence in Ruby, parentheses are mandatory around range in this case:
(0..7) % 2 #=> ((0..7).%(2)) -- as expected
0..7 % 2 #=> 0..1 -- parsed as 0..(7 % 2)
669 670 671 672 673 |
# File 'range.c', line 669
static VALUE
range_percent_step(VALUE range, VALUE step)
{
return range_step(1, &step, range);
}
|