# Communication between objects

**URL:** <https://www.ruby-forum.com/t/communication-between-objects/69212>\
**Category:** Ruby\
**Created:** [September 19, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/communication-between-objects/69212 "2006-09-19T13:12:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tod](https://www.ruby-forum.com/u/tod)\
**Post date:** [September 19, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/communication-between-objects/69212/1 "2006-09-19T13:12:38Z")

</div>

I have been working with Ruby for a few months now and have hit upon  
something that I can’t find the answer to: what does the code look like  
for passing messages back and forth between objects. Here is a specific  
example: In a web application (I am not using ROR) I have a CGI object  
and an object whose superclass is Active Record. The CGI object  
receives some params from POST and makes them available to the Active  
Record object. Not surprisingly, the Active Record object does some  
work with a database. Here’s where I am stumped how do I have the  
ActiveRecord Object tell the CGI object that it is done working with  
the database and the CGI object may now do something else.

I have made a particularly in-elegant work-around by having the ActRec  
object set a global variable to something when it is done and follow  
this with a conditional that checks to see if that global variable was  
set and if it is then the CGI object can proceed. But there has to be a  
more direct way to do this. Also, if I wanted to pass things back and  
forth between objects this would not work.

I am sure the examples are out there so I must be looking in the wrong  
place or trying to call it something it is not.

Thank you. Tod

---

<div class="post-metadata">

**Author:** ![tod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tod](https://www.ruby-forum.com/u/tod)\
**Post date:** [September 19, 2006, 1:36pm UTC](https://www.ruby-forum.com/t/communication-between-objects/69212/2 "2006-09-19T13:36:40Z")

</div>

Tod wrote:

/ …

> I am sure the examples are out there so I must be looking in the wrong  
> place or trying to call it something it is not.

Create a superclass that both objects inherit, and have the superclass  
take  
responsibility for communication between objects.

---

<div class="post-metadata">

**Author:** ![tod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tod](https://www.ruby-forum.com/u/tod)\
**Post date:** [September 19, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/communication-between-objects/69212/3 "2006-09-19T16:35:20Z")

</div>

Hi –

On Tue, 19 Sep 2006, Tod wrote:

> I have made a particularly in-elegant work-around by having the ActRec  
> object set a global variable to something when it is done and follow  
> this with a conditional that checks to see if that global variable was  
> set and if it is then the CGI object can proceed. But there has to be a  
> more direct way to do this. Also, if I wanted to pass things back and  
> forth between objects this would not work.
> 
> I am sure the examples are out there so I must be looking in the wrong  
> place or trying to call it something it is not.

I can see what kind of problem you’re trying to solve, but in this  
case I can’t seem to see beyond:

ar\_object.do\_something  
cgi\_object.do\_something\_next

which means I’m not seeing how the program is designed. Can you  
elaborate? It might even help to see the global variable version, so  
that I can see where in the code the two objects are and why they have  
to be aware of each other.

David

---

<div class="post-metadata">

**Author:** ![tod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tod](https://www.ruby-forum.com/u/tod)\
**Post date:** [September 19, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/communication-between-objects/69212/4 "2006-09-19T17:46:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

> to be aware of each other.
> 
> David

Here is the part of the code I am interested in:

require ‘cgi’  
require ‘rubygems’  
require\_gem ‘activerecord’

comment\_cgi = CGI.new

@article = comment\_cgi[‘article’]  
@user = comment\_cgi[‘user’]  
@comment = comment\_cgi[‘comment’]

class Comment \< ActiveRecord::Base  
after\_save :redirect

```
def redirect
	$ok = 'yes'
end

```

end

new\_Comment = Comment.create(  
:article\_id =\> @article,  
:user\_id =\> @user,  
:comment =\> @comment)

if $ok == ‘yes’  
comment\_cgi.out {“foo”}  
else  
comment\_cgi.out {“problem.”}  
end

So what I am doing here is “after\_save :redirect” is setting the global  
variable $ok to “yes” if the Comment object was successful in its  
dealings with the database. Then the conditional statement turns  
control back over to the CGI object allowing it to proceed (ultimately  
with a page redirect).

I know I am reinventing the wheel here because Rails make this kind of  
thing so easy but I am avoiding Rails at this point so I can really dig  
into the Ruby language and understand it.

Thank you. TOD

---

<div class="post-metadata">

**Author:** ![tod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tod](https://www.ruby-forum.com/u/tod)\
**Post date:** [September 21, 2006, 4:08am UTC](https://www.ruby-forum.com/t/communication-between-objects/69212/5 "2006-09-21T04:08:24Z")

</div>

Observable Pattern?
