DEV Community

Discussion on: Level Up Your Ruby Skillz: Working With Arrays

Collapse
 
kdraypole profile image
Kobe Raypole

Awesome article! I use the with_index method quite often. Another option is each_with_index.

At first, there appears to be no difference until you take into account that with_index accepts an optional "start index". Therefore you can start your array index at 1 by doing

result = ['a', 'b', 'c'].map.with_index(1) do |letter, index|
  "#{letter}:#{index}"
end
# result = ["a:1", "b:2", "c:3"]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
molly profile image
Molly Struve (she/her)

WHAT?!!!!!! I had no clue, thank you for sharing!!!