From: Josh Cheek Date: 2013-07-02T13:06:49+09:00 Subject: Re: How do I make lots of classes aware of each other? --047d7b34419a47dbc504e07f7995 Content-Type: text/plain; charset=ISO-8859-1 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/. > > Initialize Baz with the foo and the bar: class Foo def method puts "hi from Foo" end end class Bar def method puts "hi from Bar" end end class Baz def initialize(methodables) @methodables = methodables end def call @methodables.each { |methodable| methodable.method } end end baz = Baz.new [Foo.new, Bar.new] baz.call # >> hi from Foo # >> hi from Bar --047d7b34419a47dbc504e07f7995 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Jul 1, 2013 at 7:14 PM, Andrew S. <lists@ruby-forum.com>= 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
=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/.


Initialize Baz with the foo and t= he bar:


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

class Bar
=A0 def method
<= div>=A0 =A0 puts "hi from Bar"
=A0 end
end

class Baz
=A0 def initialize(methodables)<= /div>
=A0 =A0 @methodables =3D methodables
=A0 end
=A0=A0
=A0 def call
=A0 =A0 @methodables.each { |methodable| meth= odable.method }
=A0 end
end

ba= z =3D Baz.new [Foo.new, Bar.new]
baz.call
# >> hi from Foo
# >> hi from= Bar

--047d7b34419a47dbc504e07f7995--