How to override a method defined in Ruby module

S

Sonar Volta

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

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

Thanks in advance,
Alex
 
R

Robert Klemme

2008/4/7 said:
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

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
 
S

Sonar Volta

Robert said:
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?
 
R

Rick DeNatale

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.
 
G

gnana prasad

[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

*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top