From: Rob Biedenharn Date: 2007-06-29T08:53:49+09:00 Subject: Re: newby non/programmer trying to understand classes On Jun 28, 2007, at 6:45 PM, weathercoach@gmail.com wrote: > Hello. > ... > I've scaled down my project to the most basic components that will > still allow me to express my areas of confusion. I have a file that > changes daily which contains a list of people in this format > > bob.smith > bill.johnson > betty.joe > tony.johnson > wilbur.smith > > Additionally I have a directory populated with 1 file per uniq last > name (ie: smith, johnson, joe). I need to parse through the days list > and then cleanse the last name files of the previous entries and > insert the updated list. So when you're done, you have "==>filename<==" followed by content lines like: ==>smith<== bob.smith wilbur.smith ==>johnson<== bill.johnson ==>joe<== betty.joe Is there any reason that you can't wipe the directory clean (remove all the previous lastname files) when you start? > I've got the script that does this it is > not very efficient (ie: i must define all the last names in > advance). So now i've started to rewrite the script and this is > where I'm stuck > > ... > nf = File.open("people", "r") > surnames = [] > nf.each_line do |l| > p = LastName.new(l) > sur = p.get_last > sur << l > surnames << sur > end > > surnames.uniq.each do |s| > namefile = File.open(s,"w+") > namefile << s > end > > ... Let's translate the filename/content structure that you want to end with into a simple key/value hash: { "smith" => [ "bob.smith", "wilbur.smith" ], "johnson" => [ "bill.johnson" ], "joe" => [ "betty.joe" ] } ...and then build that: # create a Hash where referencing a non-existent # key makes its value an empty Array names = Hash.new { |h,k| h[k] = [] } File.open('people', 'r') do |f| f.each do |name| name.chomp! # remove the line-ending # so "first.mi.last" will be ["first", "mi.last"] first_name, last_name = name.split('.', 2) names[last_name] << name end end > Anyhoo I'd appreciate comments on my specific dynamic variable > problem or any other pointers > to help my get to the "AH HA!" moment with constructing and using > classes which I feel I'm pretty close to. > TIA. G > > ps: I own many ruby books and have spent about 20 hours trying to > solve this pretty basic situation and dont know where else to go for > help. Then you just need to create the output. A simple way to check in irb (you do use irb, right?) might be: names.each {|last,ary| puts "#{last}:\n #{ary.join("\n ")}" } But, if you need to remove all the existing files in the directory and write out new ones, you'll want something like: (note that I've assumed that the 'people' file and the script itself live in a directory other than where you'll put the lastname files [which is a subdirectory I called 'work']) #!/usr/bin/env ruby -w names = Hash.new { |h,k| h[k] = [] } File.open('people', 'r') do |f| f.each do |name| name.chomp! # so "first.mi.last" will be ["first", "mi.last"] first_name, last_name = name.split('.', 2) names[last_name] << name end end Dir.chdir('work') do |d| File.delete(*Dir['*']) names.each do |last,ary| File.open(last, 'w') do |lname| lname.puts ary.join("\n") end end end __END__ Now, that doesn't deal with a new class of your own making, but you don't really seem to need anything beyond some standard classes (Hash, Array, File, Dir). This should give you plenty to think (and flip through your Pickaxe) about. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com