End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Enumerable#reverse_each
enum.reverse_each(*args) { |item| block } → enum
enum.reverse_each(*args) → an_enumerator
enum.c
View on GitHub ↗
static VALUE
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
{
VALUE ary;
long len;
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
ary = enum_to_a(argc, argv, obj);
len = RARRAY_LEN(ary);
while (len--) {
long nlen;
rb_yield(RARRAY_AREF(ary, len));
nlen = RARRAY_LEN(ary);
if (nlen < len) {
len = nlen;
}
}
return obj;
}
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
Example
(1..3).reverse_each { |v| p v }
produces:
Example
3 2 1