A
anne001
I wrote a small program the procedural way, now I would like to write
one the OOP way, but
I am blocking on the matter of communication.
One way to communicate is to have a class variable, ie a global to the
class.
So lets say I define a class with two class variables and a storage
array
class dataClass
def initialize
@@Arr=array read from file, constant
@@ind=0
@storeAr=[]
end
end
I can create two objects (two instances)
datainst=dataClass.new
infoinst=dataClass.new
As I understand it the objects datainst and infoinst both have access
to @@Arr and @@ind,
and each have their own array.
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 other object ????? HOW DO I DO THAT?
break out of all recursions
end
end
end
The object datainst can call itself with Self I believe but how does
object datainst tell infoinst
to do something? Methods are written in a class, and when one writes a
class, instances don't yet exist.
The examples in my programming ruby always seem to be "me" sending a
"message" to an object, not an object talking to another object? How
can an object talk to another object?
Ruby and OOP beginner missing something
one the OOP way, but
I am blocking on the matter of communication.
One way to communicate is to have a class variable, ie a global to the
class.
So lets say I define a class with two class variables and a storage
array
class dataClass
def initialize
@@Arr=array read from file, constant
@@ind=0
@storeAr=[]
end
end
I can create two objects (two instances)
datainst=dataClass.new
infoinst=dataClass.new
As I understand it the objects datainst and infoinst both have access
to @@Arr and @@ind,
and each have their own array.
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 other object ????? HOW DO I DO THAT?
break out of all recursions
end
end
end
The object datainst can call itself with Self I believe but how does
object datainst tell infoinst
to do something? Methods are written in a class, and when one writes a
class, instances don't yet exist.
The examples in my programming ruby always seem to be "me" sending a
"message" to an object, not an object talking to another object? How
can an object talk to another object?
Ruby and OOP beginner missing something