[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

once again looking for my module methods

Trans

3/20/2006 1:03:00 PM

Gee. Once again I want my module methods inherited.

25 Answers

Robert Klemme

3/20/2006 1:44:00 PM

0

Trans wrote:
> Gee. Once again I want my module methods inherited.

How are they not?

robert

James Gray

3/20/2006 1:55:00 PM

0

On Mar 20, 2006, at 7:48 AM, Robert Klemme wrote:

> Trans wrote:
>> Gee. Once again I want my module methods inherited.
>
> How are they not?

>> module Example
>> def self.i_am_a_module_method
>> puts "Hello"
>> end
>> end
=> nil
>> include Example
=> Object
>> i_am_a_module_method
NameError: undefined local variable or method `i_am_a_module_method'
for main:Object
from (irb):10
from :0

James Edward Gray II


Edwin van Leeuwen

3/20/2006 2:58:00 PM

0

Trans wrote:
> Gee. Once again I want my module methods inherited.

I agree, I was actually going to look into it why it didn't work for me,
but now I know it's not my fault, but actually a problem with ruby :) So
thanks for saving me a couple of minutes of frustration :)


--
Posted via http://www.ruby-....


Iain D Broadfoot

3/20/2006 3:14:00 PM

0

Edwin van Leeuwen said something:
> Trans wrote:
>> Gee. Once again I want my module methods inherited.
>
> I agree, I was actually going to look into it why it didn't work for me,
> but now I know it's not my fault, but actually a problem with ruby :) So
> thanks for saving me a couple of minutes of frustration :)

One naive and ugly solution is:

module Foo
def self.included c
c.instance_eval{
def self.foo
:foo
end

def self.bar
:bar
end
}
end
end

which works, but it's far from perfect.

iain

--
"If sharing a thing in no way diminishes it, it is not
rightly owned if it is not shared." -- St. Augustine
#rm -rf /
http://www.ge...

Trans

3/20/2006 3:44:00 PM

0

Actually, I have developed what is probably the most complete solution
out there. Check out Calibre's

require 'calibre/classinherit'

Even so, any solution is still an ugly hack and imperfect to the real
solution. Hint. Hint.

---- classinherit.rb

#:title: ClassInherit
#
# This framework provides a very convenient way to have modules
# pass along class methods in the inheritance chain.
#
# Presently in Ruby the class/module methods of a module
# are not inherited when a module is included --contrary to
# the behavior of classes themselves when they are subclassed.
# To achieve the same behavior with modules requires some clever
# Ruby karate. ClassInherit provides a nice solution.
# Simply place the class inheritable methods in the block
# parameter of the special module method ClassInherit.
#
# module Mix
# def inst_meth
# puts 'inst_meth'
# end
#
# ClassInherit do
# def class_meth
# "Class Method!"
# end
# end
# end
#
# class X
# include Mix
# end
#
# X.class_meth #=> "Class Method!"
#
# ClassInherit is a capitalized method. This is used because it
# indeed creates (or reopens) a ClassInherit module in which
# the given block is evaluated, then the ClassInherit module
# is extended against the current module.
#
# The above is actually equivalent to putting the class/module
# methods in a nested ClassInherit module and extending the
# module _manually_, eg.
#
# module Mix
# def inst_meth
# puts 'inst_meth'
# end
#
# module ClassInherit
# def class_meth
# "Class Method!"
# end
# end
#
# extend ClassInherit
# end
#
# class X
# include Mix
# end
#
# X.class_meth #=> "Class Method!"
#
# Lastly, #class_inherit is an available alias for #ClassInherit
# if you prefer only lowercase methods.
#
# == Notes
#
# Just a quick comment on the need for this behavior.
#
# A module is an encapsulation of code, hence when a module is included
# (or extends), the module itself should have discretion over how it
# effects the receiving class/module. That is the very embodiment of
# encapsulation. Having it otherwise, as Ruby now does, stymies the
# practice --and we end up with "hacks" like this to compensate.
#
# Ruby would be much improved by making this bevaivor standard.
# And making non-inheritance the exception, which is alwasy easy
# enough to achieve: just put the code in a separate
# (and thus uninherited) module.
#
# == Author(s)
#
# * Thomas Sawyer
# * Nobu Nakada
# * Ulysses
#

class Module

alias_method :append_features_without_classinherit, :append_features

def append_features( base )
result = append_features_without_classinherit( base )
if const_defined?( :ClassInherit )
base.extend( self::ClassInherit )
unless base.is_a?( Class )
unless base.const_defined?( :ClassInherit )
base.const_set( :ClassInherit, Module.new )
end
my = self
base::ClassInherit.class_eval do
include my::ClassInherit
end
end
end
result
end

def ClassInherit( &yld )
if const_defined?( :ClassInherit )
self::ClassInherit.class_eval( &yld )
else
self.const_set( :ClassInherit, Module.new( &yld ) )
end
extend( self::ClassInherit )
self::ClassInherit
end

# For compatibility with old rendition
alias_method :class_inherit, :ClassInherit

end

class Class
undef_method :ClassInherit
undef_method :class_inherit
end

dblack

3/20/2006 3:46:00 PM

0

Iain D Broadfoot

3/20/2006 4:09:00 PM

0

dblack@wobblini.net said something:
> You're taking the long road :-)
>
> module Foo
> def self.included(c)
> def c.foo
> :foo
> end
> end
> #...
> end

Ah, I made the classic mistake of simplifying working code to use as an
example. :)

I do more than just create methods, so the instance_eval is useful for
me. (and I'm not refactoring my code for a while, so if I've missed
something reeeeealy simple don't tell me!)

iain

--
"If sharing a thing in no way diminishes it, it is not
rightly owned if it is not shared." -- St. Augustine
#rm -rf /
http://www.ge...

Andrew Thompson

3/20/2006 5:17:00 PM

0

There's also Hyperextend:
http://redhanded.hobix.com/bits/hyperext... which is what I ended
up using :)

Vag

--
Posted via http://www.ruby-....


Ross Bamford

3/20/2006 5:35:00 PM

0

On Mon, 2006-03-20 at 22:03 +0900, Trans wrote:
> Gee. Once again I want my module methods inherited.

As usual, I have a feeling the answer is 'no', but doesn't this take
care of it (maybe with improved ignorance of callback methods)?

module Foo
def self.included(c)
singleton_methods.reject { |e| e == 'included' }.each do |m|
mod = self
c.class_eval do
(class << self; self; end).class_eval do
define_method(m, &(mod.method(m)))
end
end
end
nil
end

def self.foo
:foo
end

def self.bar(baz)
baz
end

def joe
:joe
end
end

class Baz
include Foo
end

p Baz.foo
# => :foo
p Baz.bar(:bar)
# => :bar
p Baz.new.joe
# => :joe

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Ara.T.Howard

3/20/2006 6:01:00 PM

0