module / inheritance question

J

Joe Van Dyk

class Parent
def foo
puts "Parent::foo called"
end
end

module TheModule=20
def self.boo
foo
end
end

class Child < Parent
include TheModule
def initialize
super
end

def boo
TheModule.boo
end
end

Child.new.boo

# Two questions:
# 1) Why do I need Child.boo? Shouldn't function TheModule.boo be mixed i=
n
# to Child?
#
# 2) Any way I can get TheModule.boo to call Parent.foo?
#
#
# What I'm really trying to do is something like this:

module MenuBarUtils
def create_menu title
# Creates a Gtk::Menu and then calls #append, a function that's in
# Gtk::MenuBar.
end
def add_menu_item menu, title, &action
# create menu item, attach it to menu, and bind &action to it
end
end

class MyMenuBar < Gtk::MenuBar
include MenuBarUtils
def initialize
super
file_menu =3D create_menu "File"
add_menu_item(file_menu, "Quit") { exit }
end
end
 
Z

Zach Dennis

Joe said:
class Parent
def foo
puts "Parent::foo called"
end
end

module TheModule
def self.boo
foo
end
end

class Child < Parent
include TheModule
def initialize
super
end

def boo
TheModule.boo
end
end

Child.new.boo

# Two questions:
# 1) Why do I need Child.boo? Shouldn't function TheModule.boo be mixed in
# to Child?

Because you defined boo as a module method on TheModule. Instance
methods are mixed into classes:
module TheModule
def boo
end
end

# 2) Any way I can get TheModule.boo to call Parent.foo?

class Parent
def foo
"foo"
end
end

module M
def boo
foo
end
end

class Child < Parent
include M
end

Child.new.boo => "foo"

#
#
# What I'm really trying to do is something like this:

module MenuBarUtils
def create_menu title
# Creates a Gtk::Menu and then calls #append, a function that's in
# Gtk::MenuBar.
end
def add_menu_item menu, title, &action
# create menu item, attach it to menu, and bind &action to it
end
end

class MyMenuBar < Gtk::MenuBar
include MenuBarUtils
def initialize
super
file_menu = create_menu "File"
add_menu_item(file_menu, "Quit") { exit }
end
end

this appears to work fine... just change the definition of TheModule.boo
to TheModule#boo

Zach
 
T

Trans

Joe said:
class Parent extend self
def foo
puts "Parent::foo called"
end
end

module TheModule
def boo
Parent.foo
end
end

class Child < Parent
include TheModule
end

Child.new.boo

# Two questions:
# 1) Why do I need Child.boo? Shouldn't function TheModule.boo be mixed in
# to Child?

Include brings in the instance-level methods not the module-level
methods.
# 2) Any way I can get TheModule.boo to call Parent.foo?

'extend self' can be used to put instance-level methods on the
module-level too.

HTH,
T.
 
J

Joe Van Dyk

=20
Because you defined boo as a module method on TheModule. Instance
methods are mixed into classes:
module TheModule
def boo
end
end
=20
=20
=20
class Parent
def foo
"foo"
end
end
=20
module M
def boo
foo
end
end
=20
class Child < Parent
include M
end
=20
Child.new.boo =3D> "foo"
=20
=20
=20
this appears to work fine... just change the definition of TheModule.boo
to TheModule#boo


Ah, thanks.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top