"apple", :b=>"bear"} I think about it to do this: h = a.injec...">Geek Answers HandbookRuby hash arrayInput:a = [[:a, "apple"], [:b, "bear"]] Output: {:a=>"apple", :b=>"bear"} I think about it to do this: h = a.inject({}){|dic,i| dic.merge({i[0]=>i[1]})} But I still think this is not the best way. Does anyone have any better solutions?+4rubyHanfei sun Dec 13 '12 at 5:04source share1 answer >> Hash[*a.flatten] => {:a=>"apple", :b=>"bear"} Or more beautiful: >> Hash[a] Or after 2.1: >> a.to_h +10halfelf Dec 13 '12 at 5:10source shareMore articles:Post to Drupal with facebook account via Rest Server - androidExtjs 4.1 many-to-many model association - extjs4.1Using Python with a script (on Mac) - pythonjQuery.getJSON: how to avoid json file request with every update? (Caching) - jsonHow do you update all column values ββin Cassandra without specifying keys? - cassandraIn what order is the method called when you have a class hierarchy? - c ++How to split delimited file into POJO - javaExport to PDF from GridView - c #Multiple components in one JavaFX tableview column - javafxDisplay the contents of an array in a column dynamically with perl - arraysAll Articles
Input:
a = [[:a, "apple"], [:b, "bear"]]
Output:
{:a=>"apple", :b=>"bear"}
I think about it to do this:
h = a.inject({}){|dic,i| dic.merge({i[0]=>i[1]})}
But I still think this is not the best way. Does anyone have any better solutions?
>> Hash[*a.flatten] => {:a=>"apple", :b=>"bear"}
Or more beautiful:
>> Hash[a]
Or after 2.1:
>> a.to_h