Array to Hash

nobu.nokada@softhome.net [mailto:nobu.nokada@softhome.net] wrote simply:

h2 = a.to_hash # or a.to_h
{“x”=>1, “y”=>3}

module Enumerable
def to_h
inject({}) {|h, (k, v)| h[k] = v; h}
end
end

simply amazing solution fr the humble Nobu.
Now, I can do:

module Enumerable
def to_h
inject({}) {|h, (k, v)| h[k] = v; h}
end
end
==>nil
h = {“x”=>1,“y”=>2}
==>{“x”=>1, “y”=>2}
a = h.to_a
==>[[“x”, 1], [“y”, 2]]
h2 = a.to_h
==>{“x”=>1, “y”=>2}

Now that is Ruby.


Nobu Nakada

thank you and
kind regards -botp