Logo

Programming-Idioms

History of Idiom 7 > diff from v58 to v59

Edit summary for version 59 by programming-idioms.org:
[Ruby] One-liner is fine. Three-liner already exists in other impl.

Version 58

2017-04-15, 18:11:09

Version 59

2017-04-17, 18:51:17

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Code
items.each_with_index do |item, i| 
  puts "Item: #{item}, index: #{i}"
end
Code
items.each_index{|i| puts "Item %d = %s" % [i, items[i]]}