Ruby 3 · Basics
Loop with an index
Iterate with each_with_index, or each_with_object to build a result.
%w[a b c].each_with_index { |letter, i| puts "#{i}: #{letter}" }
Ruby 3 · Basics
Iterate with each_with_index, or each_with_object to build a result.
%w[a b c].each_with_index { |letter, i| puts "#{i}: #{letter}" }
Was this page helpful?