[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to override a method defined in Ruby module

Sonar Volta

4/7/2008 9:55:00 AM

Greetings,

I'm new in Ruby, so my question might seem a bit stupid. I have a
third-party module wired into my project (ActiveScaffold). It defines
some empty method:

[code]
module ActiveScaffold::Actions

module Update

......

# override this method if you want to do something after the save
def after_update_save(record); end

end
end

[/code]

which I need to override. How do I do this?

Thanks in advance,
Alex
--
Posted via http://www.ruby-....

5 Answers

Robert Klemme

4/7/2008 11:26:00 AM

0

2008/4/7, Sonar Volta <buistov@softline.kiev.ua>:
> Greetings,
>
> I'm new in Ruby, so my question might seem a bit stupid. I have a
> third-party module wired into my project (ActiveScaffold). It defines
> some empty method:
>
> [code]
> module ActiveScaffold::Actions
>
> module Update
>
> ......
>
> # override this method if you want to do something after the save
> def after_update_save(record); end
>
> end
> end
>
> [/code]
>
> which I need to override. How do I do this?

You simply define it in the class that includes this module. Or you
define it in a module that inherits this module.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Sonar Volta

4/7/2008 11:34:00 AM

0

Robert Klemme wrote:
> 2008/4/7, Sonar Volta <buistov@softline.kiev.ua>:
>>
>> which I need to override. How do I do this?
> You simply define it in the class that includes this module. Or you
> define it in a module that inherits this module.
>
> Kind regards
>
> robert

Ok thanks!

Is there module inheritance in Ruby?
--
Posted via http://www.ruby-....

Rick DeNatale

4/7/2008 11:45:00 AM

0

On Mon, Apr 7, 2008 at 7:33 AM, Sonar Volta <buistov@softline.kiev.ua> wrote:

> Is there module inheritance in Ruby?

No, and yes.

You can't do

module A
end

module B < A
end

but you can do:

module B
include A
end

Which acts like a sort of inheritance.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Dober

4/7/2008 11:48:00 PM

0

On Mon, Apr 7, 2008 at 1:45 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On Mon, Apr 7, 2008 at 7:33 AM, Sonar Volta <buistov@softline.kiev.ua> wrote:
>
> > Is there module inheritance in Ruby?
>
> No, and yes.
>
> You can't do
>
> module A
> end
>
> module B < A
> end
>
> but you can do:
>
> module B
> include A
> end
>
> Which acts like a sort of inheritance.
It does indeed look like that if you ask ruby itself, very much to my
dispair :( (but that's why I have implemented traits)

irb(main):001:0> a=Module::new
=> #<Module:0xb7cf35e8>
irb(main):002:0> b=Module::new
=> #<Module:0xb7cebf78>
irb(main):003:0> b.send :include, a
=> #<Module:0xb7cebf78>
irb(main):005:0> b < a
=> true
irb(main):006:0> c=Class::new
=> #<Class:0xb7cd8edc>
irb(main):007:0> c.send :include, b
=> #<Class:0xb7cd8edc>
irb(main):008:0> c.new.is_a? a
=> true

I believe that this is misleading I might agree to
c.new.behaves_like? a
but if we go all the way to ducktyping even that is unacceptable.
And yes you guessed correctly I am an inheritance skeptic but that is
not because it is a bad feature (single inheritance) but it is not the
right feature for code reuse, it is the right feature for modeling.

Smalltalk which had no mixins suffered a lot from having to use SI for
refactoring and code reuse, Squeak implemented traits for that very
reason if I understood correctly.

Cheers
Robert
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

gnana prasad

4/25/2008 2:18:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Greetings Alex,

*Overriding the metohd in a module*

save this snippet of code as *sample.rb* file

module Sample
def display
puts "Inside a mosdule"
end
end

*write another file test.rb *

require 'sample'

class Test
include Sample
def display
puts "Inside the class"
end
end

Test.new.display # OUTPUT : Inside the class

See here *display* method in the *class Test overrides *the *display*method in
*Module Sample

*


On Mon, Apr 7, 2008 at 3:25 PM, Sonar Volta <buistov@softline.kiev.ua>
wrote:

> Greetings,
>
> I'm new in Ruby, so my question might seem a bit stupid. I have a
> third-party module wired into my project (ActiveScaffold). It defines
> some empty method:
>
> [code]
> module ActiveScaffold::Actions
>
> module Update
>
> ......
>
> # override this method if you want to do something after the save
> def after_update_save(record); end
>
> end
> end
>
> [/code]
>
> which I need to override. How do I do this?
>
> Thanks in advance,
> Alex
> --
> Posted via http://www.ruby-....
>
>