From: Thilanka Marasinghe Date: 2007-07-01T17:34:33+09:00 Subject: Re: newby non/programmer trying to understand classes ------=_Part_59597_11351554.1183278874476 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline hi I Found this site very helpful when it came to understanding OO concepts for Ruby http://poignantguide.net/ruby hope this is useful On 6/30/07, Robert Klemme wrote: > > > Hi, > > I believe Chris Pine's "Learning to program" has been suggested as an > excellent introduction to OO programming. You might want to have a look. > > My short introduction to OO programming would go like this: describe > your problem in prose. Identify important nouns, these are likely > candidates for classes. Then go from there and find out how they have > to interact. > > On 29.06.2007 00:43, weathercoach@gmail.com wrote: > > Hello. > > Brief disclaimer. I'm coming from a bourne shell back ground and > > currently all of my ruby scripts look like shell scripts where I > > create methods like i would a shell function and now that I understand > > some of the language basics (variables, loops, exceptions) I want to > > improve my skills and try to leverage the language instead of just > > brute forcing my way through problems. I'm not looking for a one > > liner wizardry solution but some help getting a better understanding > > of classes and ruby in general to change my approach to writing ruby > > scripts. > > > > 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 > > So there we have the first noun that sounds like a candidate for a > class: "people" (or as a singular "person"). Apparently a Person has at > least two attributes, namely "last name" and "given name". > > You can create a person class pretty easily using Struct like this: > > Person = Struct.new :given_name, :last_name > > > 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. 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 > > So this pretty much gives an outline of the algorithm: > > 1. read the updated list file and parse full names, remember persons in > a smart way (for fast access) > > 2. read the list of surname files > > 3. delete all surname files where you do not have a person for > > 4. recreate all surname files > > Of course, other implementations would be possible. If you go with this > one way would be to create functions or methods for each of these steps > above. > > You could, for example, place the reading of the name file into class > Person as a class method: > > def Person.parse_persons(io) > persons = Hash.new {|h, k| h[k] = []} > > io.each do |line| > if /^(\w+)\.(\w+)$/ =~ line > pers = new $1.lower, $2.lower > persons[pers.last_name] << pers > end > > end > persons > end > > This will return a Hash with last_names as keys and Person instances as > values. That way you retain all the information and can use it later > for the file deletions and creations. > > Alternatively you could create a second class Persons and implement the > parse method and all other methods in that class). Maybe there is an > even better way to do it - could be that we do not yet know the whole > story and your script has to do other tasks as well. > > Just some food for thought. HTH > > Kind regards > > robert > > ------=_Part_59597_11351554.1183278874476--