can someone improve on this multiple inheritence methodology?

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
===============================================================================
 
T

Trans

I like the fact that this gets around the module method inheritance
issue. This is how #include should work to begin with, but without the
extra baggage of modules in modules (and if neccessary add an extra
"don't inheirt" section indicator in order to have every possibility).

T.
 
A

ara.t.howard

I like the fact that this gets around the module method inheritance issue.

right. what i have is actually

module Common
end

module A
inlcude Common
end

module B
inlcude Common
end

and i want to simply say 'include A' or 'include B' and have it work. it
doesn't, of course, because one cannot factor out class methods in this way
without the self::included hack, which i seem to be writing alot. at least
this was i can write it once and my module are clear in their intent (to be
mixins).
This is how #include should work to begin with, but without the extra
baggage of modules in modules (and if neccessary add an extra "don't
inheirt" section indicator in order to have every possibility).

100% agreed.

regards.

-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
===============================================================================
 
D

dblack

Hi --

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

My main suggestion would be to rename this method. I don't think this
is an inheritance operation. At least, it seems potentially confusing
to "inherit" a module, since inheritance already means something else
and modules already do other things.


David
 
A

ara.t.howard

Hi --



My main suggestion would be to rename this method. I don't think this
is an inheritance operation. At least, it seems potentially confusing
to "inherit" a module, since inheritance already means something else
and modules already do other things.

true true. hmmm... how is it different here? you'll have all the
class_methods and all the instance_methods.... and Superclass === self with
be true...

i'm sure there must be a catch though...

can you think of a better name?

cheers.

-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
===============================================================================
 
E

Eero Saynatkari

unknown said:
true true. hmmm... how is it different here? you'll have all the
class_methods and all the instance_methods.... and Superclass === self
with
be true...

i'm sure there must be a catch though...

can you think of a better name?

Well.. #include :)
Also, of course, #import, #mix, #combine and #greyskull.

The design itself seems to be fine. An alternative would
be to create an enhanced #include which would take over
if require 'better-include' is encountered (until such
time that this makes it to the stdlib).
cheers.

-a


E
 
A

ara.t.howard

Well.. #include :)
Also, of course, #import, #mix, #combine and #greyskull.

i like mix!

maybe #mixin. anyone know if there are plans for that?

-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
===============================================================================
 
D

dblack

Hi --


But self.superclass == SomeModule won't be true, nor will anything
else that reflects the singleness of inheritance. I guess my feeling
is that since inheritance is single, calling anything non-single
inheritance is more or less definitely going to cause confusion.

I was afraid you'd ask that :)
i like mix!

maybe #mixin. anyone know if there are plans for that?

That's already in very common currency, meaning essentially an
'include' operation (as in, Array mixes in Enumerable, etc.).

I don't know... I think I'm sort of terminologied out, temporarily...
but I'll keep a few brain cells on the case :)


David
 
A

ara.t.howard

But self.superclass == SomeModule won't be true, nor will anything else that
reflects the singleness of inheritance. I guess my feeling is that since
inheritance is single, calling anything non-single inheritance is more or
less definitely going to cause confusion.

not sure about that... module appear in ancesors and work like everything
else. for example

ParentModule === self

but i have a hunch there is something different. it just doesn't spring to
mind.
I don't know... I think I'm sort of terminologied out, temporarily... but
I'll keep a few brain cells on the case :)

;-)

i'm leaning towards 'inject'

class C
inject AModule::Methods
end

-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
===============================================================================
 
I

itsme213

Ara.T.Howard said:
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

I did not understand the '**' line. Shouldn't it be:
- extend ClassMethods
- extend ModuleMethods
- include InstanceMethods
?
 
A

ara.t.howard

** > include other::ModuleMethods if defined?
other::ModuleMethods

I did not understand the '**' line. Shouldn't it be:
- extend ClassMethods
- extend ModuleMethods
- include InstanceMethods
?

i'm using ModuleMethods to hold methods which can be called on either
instances or classes - like 'module_method :foo'. so this modules needs both
to extend the current module and be included in it.

kind regards.

-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
===============================================================================
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top