Alexander Shmatko Alexander Shmatko - 1 year ago 52
Ruby Question

How to create hash out of specific words in text?

I have a file with the following content:

class Rename < ActiveRecord::Migration
def change
rename_table :users, :vendors
rename_table :places, :venues

#There could be loads of similar lines

And I need to create hash with the following structure:

{ "users" => "vendors",
"places" => "venues" }

How can I achieve this?

I can already fetch needed words using the regular expression with method scan
, but it returns

[["users", "vendors"], ["places", "venues"]]

Answer Source
[["users", "vendors"], ["places", "venues"]].to_h
 => {"users"=>"vendors", "places"=>"venues"} 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download