From: Robert Klemme Date: 2010-08-27T16:14:33+09:00 Subject: Re: File Contents into Hash Table? 2010/8/27 James Rasmussen : > file_contents = [] > f = File.open("RubyTokens.txt") or die "Unable to open file..." That line is dysfunctional. Please kick this Perlism out of your Ruby toolbox ASAP. If there is an error opening the file you'll get an exception and the default behavior is to exit the interpreter (unless you catch the exception somewhere). > f.each_line { |line| file_contents.push(line)} > > But I have no idea how to take each line and put it into a hash table. > Is there any way to just read the line in, and put the whole line in as > a hash key/value pair? You can use the approach suggested with regular expression although I would also check whether the regexp really matches. h = {} File.foreach "RubyTokens.txt" do |line| h[$1] = $2.to_i if /^\s*"([^"]*)"\s*=>\s*(\d+)\s*,\s*$/ =~ line end Note that I choose "*" for the quoted text - no idea whether the empty string is allowed in your case. Also, I allowed for whitespace inserted everywhere. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/