A
Ara.T.Howard
this provide method re-use at both the class and module level. it can be used
for a kind of multiple inheritence organizational style.
harp:~ > cat a.rb
class Module
def inherit other
extend other::ClassMethods if defined? other::ClassMethods
extend other::ModuleMethods if defined? other::ModuleMethods
include other::ModuleMethods if defined? other::ModuleMethods
include other::InstanceMethods if defined? other::InstanceMethods
include other
end
end
class A
module Methods
module ClassMethods
def foo; 42; end
end
module ModuleMethods
def bar; "forty-two"; end
end
module InstanceMethods
def foobar; 42.0; end
end
def barfoo; "FORTY-TWO"; end
end
inherit Methods
end
class B
inherit A::Methods
end
module M
inherit A::Methods
end
class C
inherit M
end
b = B::new
p B::foo
p B::bar
p b::bar
p b::foobar
p b::barfoo
c = C::new
p C::foo
p C::bar
p c::bar
p c::foobar
p c::barfoo
harp:~ > ruby a.rb
42
"forty-two"
"forty-two"
42.0
"FORTY-TWO"
42
"forty-two"
"forty-two"
42.0
"FORTY-TWO"
thoughts?
-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================
for a kind of multiple inheritence organizational style.
harp:~ > cat a.rb
class Module
def inherit other
extend other::ClassMethods if defined? other::ClassMethods
extend other::ModuleMethods if defined? other::ModuleMethods
include other::ModuleMethods if defined? other::ModuleMethods
include other::InstanceMethods if defined? other::InstanceMethods
include other
end
end
class A
module Methods
module ClassMethods
def foo; 42; end
end
module ModuleMethods
def bar; "forty-two"; end
end
module InstanceMethods
def foobar; 42.0; end
end
def barfoo; "FORTY-TWO"; end
end
inherit Methods
end
class B
inherit A::Methods
end
module M
inherit A::Methods
end
class C
inherit M
end
b = B::new
p B::foo
p B::bar
p b::bar
p b::foobar
p b::barfoo
c = C::new
p C::foo
p C::bar
p c::bar
p c::foobar
p c::barfoo
harp:~ > ruby a.rb
42
"forty-two"
"forty-two"
42.0
"FORTY-TWO"
42
"forty-two"
"forty-two"
42.0
"FORTY-TWO"
thoughts?
-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================