I would like to do a.each_with_object with index, in a better way than this:
a = %w[a b c]
a.each.with_index.each_with_object({}) { |arr, hash|
v,i = arr
puts "i is: #{i}, v is #{v}"
}
i is: 0, v is a
i is: 1, v is b
i is: 2, v is c
=> {}
Is there a way to do this without v,i = arr ?
hashdoing?each_with_object({}).with_index?!|(e,h),i|, whereeis the element of the arraya,his the hash object being created and returned andiis the index.map.with_indexis another common use ofwith_index. When you can't tackwith_indexto the end of an Enumerable method, you can precede it witheach_with_index, rather thaneach.with_index.