# How do two objects communicate?

**URL:** <https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877>\
**Category:** Ruby\
**Created:** [November 26, 2005, 1:36am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877 "2005-11-26T01:36:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 26, 2005, 1:36am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/1 "2005-11-26T01:36:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 26, 2005, 2:04am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/2 "2005-11-26T02:04:15Z")

</div>

anne001 wrote:  
…

> 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 way is to register one or more objects with another object, and have  
those objects contacted on certain events. Another approach is to  
inspect the set of known objects and invoke a method based on some  
criteria (basically, ‘broadcast’ to ObjectSpace).

class A  
def initialize( name )  
@name = name  
end  
def notify1  
puts “Where’s breakfast?”  
end

def notify2  
puts “Where’s dinner?”  
end

def feed\_me  
puts “Feed #{@name}”  
end

end

class B

def initialize  
@notify\_these = {}  
end

def register( obj, meth\_name )  
@notify\_these[obj] = meth\_name  
end

def the\_magic\_event  
@notify\_these.each do |obj, m|  
obj.send m  
end  
end

def yell\_to\_the\_crowd  
ObjectSpace.each\_object do |o|  
o.send( :feed\_me ) if o.class == A  
end  
end  
end

b= B.new

a1 = A.new( ‘a1’ )  
a2 = A.new( ‘a2’ )  
b.register( a1, :notify1 )  
b.register( a2, :notify2 )

# Contact those who requested notification

b.the\_magic\_event

# Ignore the Do Not Call list:

b.yell\_to\_the\_crowd

James

–

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 1:18am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/3 "2005-11-27T01:18:43Z")

</div>

Depending on the relationship between the classes, you may want to look  
at Observable. Observable is commonly used when you wish to have one  
class notify a number of observes of a change in the object being  
observed.

[http://www.ruby-doc.org/stdlib/](http://www.ruby-doc.org/stdlib/)

Look for information on ‘observer’.

–Dale

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 4:19am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/4 "2005-11-27T04:19:53Z")

</div>

Hi –

On Sun, 27 Nov 2005, anne001 wrote:

> object communication through messages seems at the core of OOP, and yet  
> “observer” is not in my sams teach yourself ruby’s index, it is only in  
> the reference section of programming ruby, 2nd edition. Same for send  
> which seems key to the example.
> 
> Why is something which is at the heart of OOP so little explained? I am  
> baffled.

The Observable module is a rather specialized tool, and not at all at  
the heart of Ruby’s OO model. The fact that Ruby is all about message  
sending is manifested every time you see this:

object.message

That’s one object sending a message to another object. I think you’ll  
find plenty of examples of that in “Teach Yourself Ruby” 🙂

David

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 4:03am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/5 "2005-11-27T04:03:48Z")

</div>

Thank you so much!

> **[Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials \>...](https://docs.oracle.com/javase/tutorial/java/concepts/)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

What Is an Object?  
An object is a software bundle of related variables and methods.

What Is a Message?  
Software objects interact and communicate with each other using  
messages.

object communication through messages seems at the core of OOP, and yet  
“observer” is not in my sams teach yourself ruby’s index, it is only in  
the reference section of programming ruby, 2nd edition. Same for send  
which seems key to the example.

Why is something which is at the heart of OOP so little explained? I am  
baffled.

Thank you so much for the example, and all this info

Anne

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 1:29pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/6 "2005-11-27T13:29:09Z")

</div>

Yes, I am aware of the structure.  
object.message

In discussions of OOP I read, the difference is made between  
procedural, which starts with data, and flows from action to action to  
an end point in a structure organized by time, and OOP, portrayed as  
objects linked together by actions in timeless fashion.

I think what I am discovering, is that for most of its communication,  
OOP relies on a hierarchy all the same, but a hierarchy of objects. If  
object A and object B need to communicate, they can as long as they are  
embedded in object C which can call on A and on B as needed. Something  
like that.

That seems to be what OOP demystified is explaining in its example on  
collaboration in chapter 9. Because Keogth called the super class,  
linkCourseStudent, I did not realize that it was creating a class in a  
hierarchy which could create an instance of objects of class Course and  
of class Student, and call each instance method, giving “the  
appearance” of communication.

As James B. showed me, it is possible for A and B to communicate  
without being embeded in a common hierarchical object, but it is much  
less commonly done.

Am I getting this right?

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 2:09pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/7 "2005-11-27T14:09:36Z")

</div>

Thank you Edwin van Leeuwen

OK, so it is even simpler than I thought, objects are usually organized  
in a vertical hierarchy for communication. I will go back to the books  
examples with this in mind and see how that works in practice.

thank you so much

Anne

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/8 "2005-11-27T13:39:49Z")

</div>

anne wrote:

> Am I getting this right?  
> 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 a 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 ©  
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.

Hope that clears some things up 🙂

Edwin

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 3:06pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/9 "2005-11-27T15:06:07Z")

</div>

anne001 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> object A and object B need to communicate, they can as long as they  
> are embedded in object C which can call on A and on B as needed.  
> Something like that.

Not only: there’s no need for C to be present. A can reference B or  
vice  
versa (“containment”). So every method of the container has acceess to  
the  
contained instance and can send messages to (aka “invoke methods on”)  
that  
instance.

If you view OO through the “hiearchy perspective” this is what  
differenciates OO from procedural IMHO: procedural has only the  
hierarchy of  
procedure (or function) invocations. OO does have this hiearchy of  
method  
invocations, too, but adds more, orthogonal hiearchies in different  
areas:  
at runtime there’s the object composition hiearchy (on instance contains  
or  
references another which in turn contains or references other instances  
etc. - in fact these can form arbitrary graphs not only strict  
hiearchies).  
Then there’s the hierarchy of type relations called “class hiearchy”.

> That seems to be what OOP demystified is explaining in its example on  
> collaboration in chapter 9. Because Keogth called the super class,  
> linkCourseStudent, I did not realize that it was creating a class in a  
> hierarchy which could create an instance of objects of class Course  
> and of class Student, and call each instance method, giving “the  
> appearance” of communication.
> 
> As James B. showed me, it is possible for A and B to communicate  
> without being embeded in a common hierarchical object, but it is much  
> less commonly done.

IMHO the most common situation is actually where one instance references  
another instance (in UML speak “aggregation” or “composition”, see also  
[(ootips) Association, Aggregation and Composition](http://ootips.org/uml-hasa.html) ).

One of the basic tasks of software engineering is to distribute  
responsibilities properly over the code - whatever programming paradigm  
(OO,  
procedural, functional) is used. For procedural languages this means to  
identify the sub task that is delegated to another function / procedure  
which can be invoked by several other functions / procedures. For  
example,  
if in a procedural system you wanted to have a function that calculates  
the  
hypotenuse you would delegate square root calculation to another  
function.

In OO environments it’s a good rule of thumb to identify classes by  
looking  
at nouns. If you had to model an application that deals with cars and  
their  
pieces you’d probably choose “car”, “wheel”, “engine”, “lorry”, “bus” as  
classes, where some of the typically belong into an inheritance hiearchy  
(“car” probably as base class, “lorry”, “bus”) and others usually form a  
containment hiearchy (a “car” has\_an “engine” etc.). If you managed to  
identify classes then you often also know their methods (“start” for  
“engine” etc.). Of course it’s not always that simple…

HTH

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 9:18pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/10 "2005-11-27T21:18:51Z")

</div>

speechexpert wrote:

> Anyone know how to do this?  
> I am curious to know what the solution is.  
> Thanks in advance,  
> John B

Look up the pack method in Array.

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 27, 2005, 9:14pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/11 "2005-11-27T21:14:49Z")

</div>

Anyone know how to do this?  
I am curious to know what the solution is.  
Thanks in advance,  
John B

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 3:01am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/12 "2005-11-28T03:01:54Z")

</div>

Hi –

On Mon, 28 Nov 2005, speechexpert wrote:

> At this point, the file is bigger than 19994.
> 
> What gives??
> 
> I know this is something simple, can anyone tell me what is going on?
> 
> (I tried “ab” for the mode, but didn’t help.)

“a” is going to append, so maybe if you’ve run your program twice  
you’ve actually written the data twice to the file. “w” will  
overwrite the file if it already exists, so you probably want that.

David

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 2:49am UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/13 "2005-11-28T02:49:49Z")

</div>

Hi - Thanks for previous help, very useful.

At this point I have a problem:

I run Zlib (a compression class) on a string. It outputs a string of  
bytes  
(binary) Call it bout. (bout = Zlib::Deflate.deflate(instr) )  
I go bout.length It is 19994  
Then I open a f = File.new(filename, “a”)  
I go f.write(bout)

At this point, the file is bigger than 19994.

What gives??

I know this is something simple, can anyone tell me what is going on?

(I tried “ab” for the mode, but didn’t help.)

Thanks in advance for any help!  
John B

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 1:11pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/14 "2005-11-28T13:11:26Z")

</div>

I did - but could see no way to pack a binary string.  
Is there a way to pack a binary string of bytes?  
jb

----- Original Message -----  
From: “Timothy H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 27, 2005 3:17 PM  
Subject: Re: How to read/write an int in binary form to a file?

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 1:19pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/15 "2005-11-28T13:19:34Z")

</div>

I need to write and then read a number of bytes to and from a file…  
It seems the pack method will do it, but  
does anyone know how to invoke it for a string of bytes?  
Thanks in advance,  
John B

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 1:31pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/16 "2005-11-28T13:31:43Z")

</div>

speechexpert wrote:

> I did - but could see no way to pack a binary string. Is there a way to  
> pack a binary string of bytes?  
> jb

I’m missing something, I think. You don’t need to pack a binary string.  
A binary string is already packed.

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 1:15pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/17 "2005-11-28T13:15:32Z")

</div>

As far as appending, I erase the file every time to start again.  
The problem is I n eed to pack the binary string and don’t know how.  
(yet)  
JB

----- Original Message -----  
From: “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 27, 2005 8:59 PM  
Subject: Re: Case of the missing bytes

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 3:04pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/18 "2005-11-28T15:04:51Z")

</div>

Don’t know if this’ll help;

f = File.new(filename, “ab”)

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 3:25pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/19 "2005-11-28T15:25:15Z")

</div>

On 28/11/05, speechexpert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to write and then read a number of bytes to and from a file…  
> It seems the pack method will do it, but  
> does anyone know how to invoke it for a string of bytes?  
> Thanks in advance,  
> John B

string = “some junk”

File.open(‘/tmp/junk’, ‘wb’) do | file |  
file.print(string)  
end

read = File.read(‘/tmp/junk’)

p(string == read)

–  
[http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)

Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![anne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@anne](https://www.ruby-forum.com/u/anne)\
**Post date:** [November 28, 2005, 3:37pm UTC](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877/20 "2005-11-28T15:37:25Z")

</div>

yes - the “b” did it - I had 2 bugs going and didn’t realize that fix  
sooner.  
Thanks to all,  
John B

----- Original Message -----  
From: “David B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, November 28, 2005 9:00 AM  
Subject: Re: Case of the missing bytes

Don’t know if this’ll help;

f = File.new(filename, “ab”)

[Next page](https://www.ruby-forum.com/t/how-do-two-objects-communicate/48877.md?page=2)
