Logo

Programming-Idioms

History of Idiom 7 > diff from v132 to v133

Edit summary for version 133 by programming-idioms.org:
[Ruby] Added Doc URL

Version 132

2022-08-27, 16:18:17

Version 133

2022-09-02, 22:22:45

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

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Code
items.each_with_index do |x, i| 
  puts "Item #{i} = #{x}"
end
Code
items.each_with_index do |x, i| 
  puts "Item #{i} = #{x}"
end
Comments bubble
See also the one-liner Ruby solution
Comments bubble
See also the one-liner Ruby solution
Doc URL
https://ruby-doc.org/core-3.1.2/Enumerable.html#method-i-each_with_index