From: Mike Dalessio Date: 2012-04-19T02:55:43+09:00 Subject: Re: Why must I know whether I extend a class or a module? --20cf303dd26455501c04bdf7c3f6 Content-Type: text/plain; charset=ISO-8859-1 Hi, On Wed, Apr 18, 2012 at 1:13 PM, Marc Heiler wrote: > Hi. > > I have one module and one class. > > module Foo > def test > puts 'This is method test() from module Foo.' > end > end > > class Bar > def test > puts 'This is method test() from class Bar.' > end > end > > > If I want to modify that at a later time, I need > to do something like: > > module Foo > def test > puts 'Yes, still testing'. > end > end > > Or > > class Bar > # etc.. > > My question is: > > - Why do I need to KNOW that it is a module or a class? > > Can't I simply say "I do not care what it is a class or a module, simply > add this method to it."? > > I don't understand why I need to explicitely know that one is a module > and the other is a class. > > Was there a reason this distinction was used? > > I don't have a good alternative though. I thought about: > > modify Bar > modify Class > > but that would require a new keyword I suppose. And typing "modify" is > also longer than typing class. So there is no real advantage. > > So my real question is not so much why things are the way they are or > that I want a change (I don't think there is a good change for it, so > the current way is probably good enough), but my question is: > > - Why are things this way? Does anyone know? > > PS: On the other hand, when we can god-patch a class or a module, and > undo these changes, perhaps we could also overcome the separation > between classes and modules when I want to modify their behaviour. From > a design point of view, I dont really think it should be needed to know > whether one modifies a module or a class. (Ruby's behaviour could be to > first search for class, then for module.) > > You may be able to use `class_eval`: #! /usr/bin/env ruby class Foo def self.quux puts "Foo#quux" end end module Bar def self.quux puts "Bar#quux" end end Foo.quux # => "Foo#quux" Bar.quux # => "Bar#quux" Foo.class_eval do def self.quux puts "Foo overridden" end end Bar.class_eval do def self.quux puts "Bar overridden" end end Foo.quux # => "Foo overridden" Bar.quux # => "Bar overridden" --20cf303dd26455501c04bdf7c3f6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi,

On Wed, Apr 18, 2012 at 1:13 PM, Marc= Heiler <lists= @ruby-forum.com> wrote:
Hi.

I have one module and one class.

module Foo
=A0def test
=A0 =A0puts 'This is method test() from module Foo.'
=A0end
end

class Bar
=A0def test
=A0 =A0puts 'This is method test() from class Bar.'
=A0end
end


If I want to modify that at a later time, I need
to do something like:

module Foo
=A0def test
=A0 =A0puts 'Yes, still testing'.
=A0end
end

Or

class Bar
=A0# etc..

My question is:

- Why do I need to KNOW that it is a module or a class?

Can't I simply say "I do not care what it is a class or a module, = simply
add this method to it."?

I don't understand why I need to explicitely know that one is a module<= br> and the other is a class.

Was there a reason this distinction was used?

I don't have a good alternative though. I thought about:

modify Bar
modify Class

but that would require a new keyword I suppose. And typing "modify&quo= t; is
also longer than typing class. So there is no real advantage.

So my real question is not so much why things are the way they are or
that I want a change (I don't think there is a good change for it, so the current way is probably good enough), but my question is:

- Why are things this way? Does anyone know?

PS: On the other hand, when we can god-patch a class or a module, and
undo these changes, perhaps we could also overcome the separation
between classes and modules when I want to modify their behaviour. From
a design point of view, I dont really think it should be needed to know
whether one modifies a module or a class. (Ruby's behaviour could be to=
first search for class, then for module.)


You may be able to use `class_eval`:

<= /div>
#! /usr/bin/env ruby

class Foo
=A0 def self.quux
=A0 =A0 puts "Foo#quux"
=A0 end
end

module Bar
=A0 d= ef self.quux
=A0 =A0 puts "Bar#quux"
=A0 end<= /div>
end

Foo.quux # =3D> "Foo#quux"
Bar.q= uux # =3D> "Bar#quux"

Foo.class_eval = do
=A0 def self.quux
=A0 =A0 puts "Foo overridden&= quot;
=A0 end
end

Bar.class_eval do
=
=A0 def self.quux
=A0 =A0 puts "Bar overridden"
=A0 end
end

Foo.quux # =3D> &= quot;Foo overridden"
Bar.quux # =3D> "Bar overridden"

--20cf303dd26455501c04bdf7c3f6--