[Courses] [Ruby] Lesson 1: Classes

Laurel Fan laurel.fan at gmail.com
Tue Nov 29 05:04:11 EST 2005


Comments inline (I hope you don't mind the lack of snipping, I wanted
to preserve the whole context).

On 11/26/05, Anne G <anne at wjh.harvard.edu> wrote:
> 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?

It needs a reference to infoinst.  If you defined the lookForX
function to take another object as an argument, you could pass it
infoinst.  For example:

class dataClass
     def lookforX(other) ## this tells us that lookforX takes 'other'
as an argument
         look for X in @@arr[@@ind]
         if X not found
             increase @@ind
             Self.lookforX
         else X is found
             other.doSomethingElse ## this calls lookforX on other. 
We pass it nil because lookforX takes an argument
             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

    def doSomethingElse
    ...   ## infoinst is the same class as datainst, so we could
define the doSomethingElse method here
   end
 end

Then, when you call the lookforX method, you would do something like:

datainst.lookforX(infoinst)

This calls lookforX on datainst, and passes it a reference to
infoinst.  So, if you look at the new lookforX definition, it calls
"other.doSomethingElse".  Since other is infoinst, this calls
infoinst.doSomethingElse.

> 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?

Not necessarily.  Defining methods for objects (instead of for
classes) can sometimes be useful, but usually if we want objects to
have different methods we make them different classes.  Passing
references to other objects is the usual way of having objects talk to
each other.

--
Laurel Fan
http://dreadnought.gorgorg.org


More information about the Courses mailing list