each_with_index

enum.each_with_index(*args) { |obj, i| block } â enum
enum.each_with_index(*args) â an_enumerator
Instance Public methods

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}
doc_ruby_on_rails
2015-04-04 23:54:41
Comments
Leave a Comment

Please login to continue.