[Courses] [Ruby] Lesson 2: Iterators

Anne G anne at wjh.harvard.edu
Fri Dec 9 22:43:44 EST 2005


HI,

Thank you for your response

> I don't think this is so much a Ruby thing as it is a
> design issue.
Yes, I want to understand object oriented programming
thinking. I started with objective C and cocoa, but I was
frustrated with it and not learning any OOP. Ruby is so much
easier, I can get to the methodology.

> Is this an abstract example or is there a practical
> application?
What I want to learn is abstract, the example I chose to
learn with - which might not be the best - is concrete.

As I understand it, in procedural programming, which is what
I have been doing, I write code thinking all along of time.
The code does this, then this, then this... in a thread.

In OO programming, it is more like a state machine in
digital electronic. All I need to know is the state of the
object, and what it will do if it gets some info. So I
should be able to define things locally only, and the whole
thing should work based on logic more than time.

The concrete example I took is an archive file with 20000
lines of data from 200 subjects in an experiment. The
procedure way, I read the file line by line, and with ifs,
switches etc, I tell it what to do to extract the info I
want from the data file.  The OO P way, I have three
objects, each specializing in a particular aspect of the
data, and they call on each other once they are done with
their part.

> fruits = ['apple', 'pear', 'banana'] # an array of
> fruits fruits.first # send the message #first to the
> fruits object. it returns a string: 'apple'

This is how objects and messages are first explained. But my
question is at one level of abstraction higher: In most
examples, the methods are defined in a class when the
instances of the objects are not yet defined.

In my case, How will the object which stores the actual
subject data tell the object which stores the subject's id
that the next line in the file is for it to process, when
neither object is around at the time of the method
definition.

Laurel came up with a class object @helper which will
contain the instantiated object to be called at execution.

This worked very well, getdata calls getinfo1 which calls
getinfo2 which calls getdata ad infinitum. The problem I
encounted is that ad infinitum is not supported by my
implementation of Ruby, I got an error message, too many
levels (each repeat call on getdata is a level).

Of course I can find ways to write the code more efficiently
to avoid the limit, but by calling a method for each line of
code, I encountered a limit, and the fact that the limit
exists is what bothers me. That means I have to make sure
the whole program is done is so many calls.  This is not a
stable timeless implementation. It could not run on live
data that kept coming and coming.

It is probably because I am doing something wrong
in my object communication, but I don't know what it is.

---------------------------------------------------------

as for the general problem as to how to get objects to
communicate, here is an answer I received, which I haven't
figured out how to use in my own 3 object example.

Partly, most objects communicate only with objects they
create and the object that created them.

class A
  def initialize
    arr=[1,2,3,4]
    arr.last
  end
  def ask
    return arr.first
  end
end

a=A.new
last=a.ask

Here the object we are currently in creates an object a of
class A, it communicates with this object with a.ask. The
object a itself creates an Object arr and also communicates
with that. This is the way most object communicate with each
other.

If you need two object (A and B) that are created by a third
object (C)  to communicate with each other then you are
probably doing something wrong (although not always) and it
might have been better if Object A had created object B
instead of letting Object C create both.





More information about the Courses mailing list