[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Overriding Class Methods With Modules

John W. Long

8/30/2006 1:36:00 PM

How do I override a class method on an object with another module? For
example:

class TestObject
def self.my_method
"override me"
end
end

module TestExtension
def my_method
"overridden by TestExtension"
end
def another_method
"another method"
end
end

TestObject.extend TestExtension

puts TestObject.my_method #=> "override me"
puts TestObject.another_method #=> "another method"

Why can't I get TestObject to use my_method from TestExtension?

--
John Long
http://wiseheart...
http://radi...

5 Answers

Trans

8/30/2006 2:04:00 PM

0


John W. Long wrote:
> How do I override a class method on an object with another module? For
> example:
>
> class TestObject
> def self.my_method
> "override me"
> end
> end
>
> module TestExtension
> def my_method
> "overridden by TestExtension"
> end
> def another_method
> "another method"
> end
> end
>
> TestObject.extend TestExtension
>
> puts TestObject.my_method #=> "override me"
> puts TestObject.another_method #=> "another method"
>
> Why can't I get TestObject to use my_method from TestExtension?

Because we don't have Cuts ;-)

Okay a more presently practical answer. You can use a hack like:

module TestExtension
def self.included(base)
base.module_eval do
def my_method
"overridden by TestExtension"
end
end
end
def another_method
"another method"
end
end

T.

John W. Long

8/30/2006 2:44:00 PM

0

Trans wrote:
> Okay a more presently practical answer. You can use a hack like:
>
> module TestExtension
> def self.included(base)
> base.module_eval do
> def my_method
> "overridden by TestExtension"
> end
> end
> end
> def another_method
> "another method"
> end
> end

Mmm. That doesn't work either.

--
John Long
http://wiseheart...
http://radi...


Jano Svitok

8/30/2006 2:52:00 PM

0

On 8/30/06, John W. Long <ng@johnwlong.com> wrote:
> Trans wrote:
> > Okay a more presently practical answer. You can use a hack like:
> >
> > module TestExtension
> > def self.included(base)

shouldn't be "extended" ?

> > base.module_eval do
> > def my_method
> > "overridden by TestExtension"
> > end
> > end
> > end
> > def another_method
> > "another method"
> > end
> > end
>
> Mmm. That doesn't work either.

dblack

8/30/2006 2:53:00 PM

0

Aleks Kissinger

8/30/2006 3:19:00 PM

0

That works. So the final code would be something like:

module TestExtension
def inject_methods(base)
base.module_eval do
def self.my_method
"overridden by TestExtension"
end
end
end

def extended(base)
inject_methods(base)
end

def included(base)
inject_methods(base)
end
end

This has always been a little clunky in Ruby, primarily because
methods written within a specific class were designed to override
mixin and parent class methods, not vice-versa.

On 8/30/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/30/06, John W. Long <ng@johnwlong.com> wrote:
> > Trans wrote:
> > > Okay a more presently practical answer. You can use a hack like:
> > >
> > > module TestExtension
> > > def self.included(base)
>
> shouldn't be "extended" ?
>
> > > base.module_eval do
> > > def my_method
> > > "overridden by TestExtension"
> > > end
> > > end
> > > end
> > > def another_method
> > > "another method"
> > > end
> > > end
> >
> > Mmm. That doesn't work either.
>
>