From: Olivier Renaud Date: 2007-08-16T03:26:08+09:00 Subject: Re: String parse Le mercredi 15 ao�t 2007 01:00, anitawa a �crit�: > Hello, > > I want to be able to parse a string and put them into variables. > > for example, I have this string: > > "Menu: steak_and_egg | date: 0814 | who: Anita" > > I want to parse this string to assign variables like so: > > menu = "steak_and_egg" > date = "0814" > who = "Anita" > > What would be the fastest way of doing this. > > Thanks This is a CSV format, You can use the csv lib from the stdlib : # parse with : and | as delimiters str = "Menu: steak_and_egg | date: 0814 | who: Anita" parsed = CSV.parse(str, ":", "|") => [["Menu", " steak_and_egg "], [" date", " 0814 "], [" who", " Anita"]] # then, put the results in a hash res = {} parsed.each do |k, v| res[k.strip] = v.strip end => {"date"=>"0814", "who"=>"Anita", "Menu"=>"steak_and_egg"} You can also use Enumerable#inject for putting in the Hash (or Hash::[] with some adaptations) -- Olivier Renaud