S
skibud2
Hey Guys,
I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).
Thanks
["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}
My Current Code:
def array_to_hash(array)
count = 0
hash = Hash.new
(array.length / 2).times do
hash[array[count]] = array[count+1]
count += 2
end
return hash
end
I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).
Thanks
["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}
My Current Code:
def array_to_hash(array)
count = 0
hash = Hash.new
(array.length / 2).times do
hash[array[count]] = array[count+1]
count += 2
end
return hash
end