Alexander Shmatko Alexander Shmatko - 5 months ago 6
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
end
end


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
/rename_table.:(\S+),\s:(\S+)/
, but it returns

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

Answer
[["users", "vendors"], ["places", "venues"]].to_h
 => {"users"=>"vendors", "places"=>"venues"}