ruby - Alternatives to convert a array to a hash using same keys and values -
i want convert:
[:one, :two, :three]
to:
{one: :one, two: :two, three: three}
so far i'm using this:
hash[[:basic, :silver, :gold, :platinum].map { |e| [e, e] }]
but know if it's possible other way?
this use in rails enum
definition in model, save values strings in db.
i admit hangup: given choice, prefer constructing hashes scratch rather creating array , converting hash.
[:one, :two, :three].each_with_object({}) { |e,h| h[e]=e } #=> {:one=>:one, :two=>:two, :three=>:three}
Comments
Post a Comment