Method: Enumerable#each_with_index
- Defined in:
- enum.c
#each_with_index(*args) {|obj, i| ... } ⇒ Enumerator #each_with_index(*args) ⇒ Object
Calls block with two arguments, the item and its index, for each item in enum. Given arguments are passed through to #each().
If no block is given, an enumerator is returned instead.
hash = Hash.new
%w(cat dog wombat).each_with_index { |item, index|
hash[item] = index
}
hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 |
# File 'enum.c', line 1725
static VALUE
enum_each_with_index(int argc, VALUE *argv, VALUE obj)
{
NODE *memo;
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
memo = NEW_MEMO(0, 0, 0);
rb_block_call(obj, id_each, argc, argv, each_with_index_i, (VALUE)memo);
return obj;
}
|