[Courses] [Ruby] Lesson 1: Classes

Anne G anne at wjh.harvard.edu
Sun Nov 27 07:53:02 EST 2005


Here is the example I am thinking about:

I define a class with two class variables and a storage
array, and I create two instances
datainst=dataClass.new
infoinst=dataClass.new

the objects datainst and infoinst both have access to @@Arr
and @@ind, and each have their own array.

class dataClass
     def initialize
        @@Arr=array read from file, constant
        @@ind=0
        @storeAr=[]
     end
end

I can get my program rolling by calling a method, say
datainst.lookforX

class dataClass
    def lookforX
        look for X in @@arr[@@ind]
        if X not found
            increase @@ind
            Self.lookforX
        else X is found
            Send a message to the other object to tell it
its its turn to do something   ?????  HOW DO I DO THAT?
            break out of all recursions
        end
    end
end

The object datainst can call itself with Self but how does
object 'datainst' tell object 'infoinst' that it is its turn
to be processing the array?

In Sams learning ruby, it says that you can define a method
for an object.
def datainst.lookforX
     ...
     else X is found
         infoinst.getinfo
         break out of all recursions
     end
   end
end
def infoinst.getinfo
...
end

Is this the way to go to get two objects to talk to each
other?

anne



More information about the Courses mailing list