I am attempting to write a method named my_transform
that takes an array as follows:
items = [\"Aqua\", \"Blue\", \"Green\", \"Red\", \"Yellow\"]
>
I would use Array#to_h:
items = ["Aqua", "Blue", "Green", "Red", "Yellow"]
items.each_with_index.to_h
#=> { "Aqua"=>0, "Blue"=>1, "Green"=>2, "Red"=>3, "Yellow"=>4 }
Note that to_h
was introduced in Ruby 2.1
Using to_h
your my_transform
method could look like this:
def my_transform(items)
items.each_with_index.to_h
end