Logo

Programming-Idioms

History of Idiom 7 > diff from v135 to v136

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

Version 135

2022-09-02, 22:27:03

Version 136

2022-09-02, 22:27:21

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
Doc URL
https://ruby-doc.org/core-3.1.2/Enumerable.html#method-i-each_with_index
Demo URL
https://replit.com/@ProgIdioms/AromaticCostlyFormats#main.rb