Method: Array#reverse_each
- Defined in:
- array.c
#reverse_each {|element| ... } ⇒ self #reverse_each ⇒ Enumerator
When a block given, iterates backwards over the elements of self, passing, in reverse order, each element to the block; returns self:
a = []
[0, 1, 2].reverse_each {|element| a.push(element) }
a # => [2, 1, 0]
Allows the array to be modified during iteration:
a = ['a', 'b', 'c']
a.reverse_each {|element| a.clear if element.start_with?('b') }
a # => []
When no block given, returns a new Enumerator.
Related: see Methods for Iterating.
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 |
# File 'array.c', line 2716
static VALUE
rb_ary_reverse_each(VALUE ary)
{
long len;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
len = RARRAY_LEN(ary);
while (len--) {
long nlen;
rb_yield(RARRAY_AREF(ary, len));
nlen = RARRAY_LEN(ary);
if (nlen < len) {
len = nlen;
}
}
return ary;
}
|