each()
public
Iterates over the elements rng, passing each in turn to the block. You can only iterate
if the start object of the range supports the succ method (which
means that you can’t iterate over ranges of Float objects).
(10..15).each do |n|
print n, ' '
end
produces:
10 11 12 13 14 15
Show source
static VALUE
range_each(VALUE range)
{
VALUE beg, end;
RETURN_ENUMERATOR(range, 0, 0);
beg = RANGE_BEG(range);
end = RANGE_END(range);
if (!rb_respond_to(beg, id_succ)) {
rb_raise(rb_eTypeError, "can't iterate from %s",
rb_obj_classname(beg));
}
if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
long lim = FIX2LONG(end);
long i;
if (!EXCL(range))
lim += 1;
for (i = FIX2LONG(beg); i < lim; i++) {
rb_yield(LONG2FIX(i));
}
}
else if (TYPE(beg) == T_STRING) {
VALUE args[2];
args[0] = end;
args[1] = EXCL(range) ? Qtrue : Qfalse;
rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0);
}
else {
range_each_func(range, each_i, NULL);
}
return range;
}