Alexander Shmatko Alexander Shmatko - 11 months ago 26
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"]]

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