instance method
Range#%
(::Numeric | ::int n) -> ::Enumerator[Elem, self]
(::Numeric | ::int n) { (Elem element) -> void } -> self
%(n) {|element| ... } → self
%(n) → enumerator or arithmetic_sequence
range.c
View on GitHub ↗
static VALUE
range_percent_step(VALUE range, VALUE step)
{
return range_step(1, &step, range);
}
Same as step (but doesn’t provide default value for n). The method is convenient for experssive producing of Enumerator::ArithmeticSequence.
Example
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:
Example
(0..7) % 2 #=> ((0..7).%(2)) -- as expected 0..7 % 2 #=> 0..1 -- parsed as 0..(7 % 2)