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"]
Awesome article! I use the
with_index
method quite often. Another option iseach_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 doingWHAT?!!!!!! I had no clue, thank you for sharing!!!