Logo

Programming-Idioms

History of Idiom 7 > diff from v48 to v49

Edit summary for version 49 by steenslag:
[Ruby] each_with_index iterates over index and value at the same time.

Version 48

2016-02-18, 16:57:56

Version 49

2016-04-18, 20:14:41

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

Code
items.each_index do |i| 
	puts "Item #{i} = #{items[i]}"
end
Code
items.each_with_index do |el, i| 
  puts "Item #{i} = #{el}"
end
Comments bubble
See also the one-liner Ruby solution
Comments bubble
See also the one-liner Ruby solution