convert array to a hash : Array Extension « Array « Ruby
- Ruby
- Array
- Array Extension
convert array to a hash
class Array
def invert
h={}
self.each_with_index{|x,i| h[x]=i}
h
end
end
a = ["red","yellow","orange"]
h = a.invert # {"orange"=>2, "yellow"=>1, "red"=>0}
Related examples in the same category