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       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools