From: David Vallner Date: 2006-02-14T03:46:44+09:00 Subject: Re: tree structures Dňa Pondelok 13 Február 2006 19:13 frank napísal: > But it seems like there is a huge gulf between my procedural > thinking and an OO thinking with regard to this tree structure. Oh, it gets worse. Data structures are actually more or less the same thing in procedural and OO, except with encapsulation applying in the simpler cases, and abstract interfaces in the more complicated ones - the actual implementation of the algorithms is identical. > How would you represent a linked list in Ruby? > Usually not at all. If you can place a reasonable upper bound on the amount of data you will be storing, the standard Ruby Array will work well - it does support the necessary operations. > In other words, would it be possible to convert the following > procedural Ruby code into OO ruby so that I would be able to "get it". > Or are there equivalent examples procedural to OO. Now this is a tricky one. I could give my personal list of "recommended" reading to see just where OO is significantly different from procedural, but I think that is a little out of your scope just now, and the basics, which are well enough documented in the books you mentioned, will do for now. > I mean, what I have written is awful looking compared to the class Node > you posted earlier...and while I understand the ideas of instance variables > and self and classes methods etc. I am struggling to get them right in my > head. > Actually, I don't think it's related to that. The code I posted, nearly identical to the Ruby Way one, wasn't that advanced, it was just granular - more on that a bit below. > This is not the full program but enough to see what I am trying to > accomplish. Not quite. I can't really see the intent of the code clearly, and I have to pore over it to find out what's happening - if I'm correct, you're trying to read the tree structure from a string. I'll give you an additional small exercise: You're doing everything in the toplevel scope with up to four levels of nested "if"s, mostly communicating via three more or less global variables. Try to break up the script into small bits. Stay with procedural for now, but break it up into functions of at most 10 lines each, preferrably only around five. And try to give them some informative names, though not too long ones. The way your script is now, it's easy to forget which of those counters was doing what after I scroll past the comments describing that, and so on. David Vallner