Logo

Programming-Idioms

History of Idiom 7 > diff from v57 to v58

Edit summary for version 58 by hakunin:
[Ruby] original code was too complicated and un-ruby like

Version 57

2017-01-25, 09:35:12

Version 58

2017-04-15, 18:11:09

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_index{|i| puts "Item %d = %s" % [i, items[i]]}
Code
items.each_with_index do |item, i| 
  puts "Item: #{item}, index: #{i}"
end