From: Robert Klemme Date: 2011-01-26T22:59:22+09:00 Subject: Re: IO from text file On Wed, Jan 26, 2011 at 1:04 PM, Michael Litton wrote: > Thanks for the excellent advice as I now have the correct information > when I run the code. I am slowly working on a program where I ask a user > for a selection, compare the selection to a file, then assign that > selection(two pieces of information) to a variable to be used later. > > In your opinion should this data file be YAML, csv, or something else? I > am only looking at less than 100 records. The format of that file does not really matter - all these are proper options. I suggest you base your decision on what's most convenient for the producer of the file (user manual typing, spreadsheet or database export, ...). If you want to ask the user multiple times and given the size of the file you should consider loading the file into a data structure which gives you efficient access (e.g. a Hash with proper key values) so you don't have to read the file repeatedly. This will be faster than having to go to the file over and over again. If you use YAML you can directly load the file into the Hash: 14:54:54 Temp$ irb19 -r yaml Ruby version 1.9.2 irb(main):001:0> h={};10.times {|i| h["key #{i}"]="value #{i}"} => 10 irb(main):002:0> h => {"key 0"=>"value 0", "key 1"=>"value 1", "key 2"=>"value 2", "key 3"=>"value 3", "key 4"=>"value 4", "key 5"=>"value 5", "key 6"=>"value 6", "key 7"=>"value 7", "key 8"=>"value 8", "key 9"=>"value 9"} irb(main):003:0> s = h.to_yaml => "--- \nkey 0: value 0\nkey 1: value 1\nkey 2: value 2\nkey 3: value 3\nkey 4: value 4\nkey 5: value 5\nkey 6: value 6\nkey 7: value 7\nkey 8: value 8\nkey 9: value 9\n" irb(main):004:0> puts s --- key 0: value 0 key 1: value 1 key 2: value 2 key 3: value 3 key 4: value 4 key 5: value 5 key 6: value 6 key 7: value 7 key 8: value 8 key 9: value 9 => nil irb(main):005:0> h2 = YAML.load(s) => {"key 0"=>"value 0", "key 1"=>"value 1", "key 2"=>"value 2", "key 3"=>"value 3", "key 4"=>"value 4", "key 5"=>"value 5", "key 6"=>"value 6", "key 7"=>"value 7", "key 8"=>"value 8", "key 9"=>"value 9"} irb(main):006:0> h == h2 => true irb(main):007:0> For reading YAML from a file you can use a 1liner: hash = File.open("data") {|io| YAML.load(io)} Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/