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}" }

Official Ruby 3 documentation

More basics commands

All basics commands · Ruby 3 cheatsheet

Was this page helpful?