From: Stu Date: 2013-07-02T09:54:03+09:00 Subject: Re: How do I make lots of classes aware of each other? --089e013d0f8ac70cbb04e07cc74d Content-Type: text/plain; charset=ISO-8859-1 You can use send which as you would expect sends messages to an object. You can build wrapper methods around that as it's a oops primative. The design pattern you may want to look at (which is just an array of instances) would be to create an observer class which enumerates through the array depending on your use case conditions will send the appropriate message to the objects within the array. And no you want to avoid using the global variables. There are sort of scoped class variables with the double at @@foo which remove the need to create the observer class but it's probably better to avoid those as well. ~Stu On Mon, Jul 1, 2013 at 7:14 PM, Andrew S. wrote: > I'm apparently missing something fundamental in my knowledge of classes > and how they interoperate. > > Let's say I have lots of classes, such as the following: > > class Foo > def method > puts "hi" > end > end > > class Bar > def method > puts "hi" > end > end > > And I want to write another class that uses all of those classes. For > example: > > class Baz > foo.method > bar.method > end > > What I'm doing right now is initiating all the classes as follows: > > $foo = Foo.new > $bar = Bar.new > > So I then have: > > class Baz > $foo.method > $bar.method > end > > But this has to be wrong (?) > > I could make them all modules and then include them as needed, but I'd > still end up with: > > class baz > include Foo > include Bar > ...x20ish > > Essentially, I have about 20 classes and many of them talk to each > other, so they have to be aware of each other. What's the right way to > structure all of this? > > Many thanks in advance! > > Andrew > > -- > Posted via http://www.ruby-forum.com/. > > --089e013d0f8ac70cbb04e07cc74d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
You can use send which as you would expect sends= messages to an object. You can build wrapper methods around that as it'= ;s a oops primative. The design pattern you may want to look at (which is j= ust an array of instances) would be to create an observer class which enume= rates through the array depending on your use case conditions will send the= appropriate message to the objects within the array.

And no you want to avoid using the global variables. There are so= rt of scoped class variables with the double at @@foo which remove the need= to create the observer class but it's probably better to avoid those a= s well.

~Stu



On Mon, Jul 1, 2013 at 7:14 PM, Andrew= S. <lists@ruby-forum.com> wrote:
I'm apparently missing something fundame= ntal in my knowledge of classes
and how they interoperate.

Let's say I have lots of classes, such as the following:

class Foo
=A0 def method
=A0 =A0 puts "hi"
=A0 end
end

class Bar
=A0 def method
=A0 =A0 puts "hi"
=A0 end
end

And I want to write another class that uses all of those classes. =A0For example:

class Baz
=A0 foo.method
=A0 bar.method
end

What I'm doing right now is initiating all the classes as follows:

$foo =3D Foo.new
$bar =3D Bar.new

So I then have:

class Baz
=A0 $foo.method
=A0 $bar.method
end

But this has to be wrong (?)

I could make them all modules and then include them as needed, but I'd<= br> still end up with:

class baz
=A0 include Foo
=A0 include Bar
=A0 ...x20ish

Essentially, I have about 20 classes and many of them talk to each
other, so they have to be aware of each other. =A0What's the right way = to
structure all of this?

Many thanks in advance!

Andrew

--
Posted via http://= www.ruby-forum.com/.


--089e013d0f8ac70cbb04e07cc74d--