T
Trans
Gee. Once again I want my module methods inherited.
Trans said:Gee. Once again I want my module methods inherited.
NameError: undefined local variable or method `i_am_a_module_method'How are they not?
Trans said: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
Edwin van Leeuwen said something:
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.
You're taking the long road
module Foo
def self.included(c)
def c.foo
:foo
end
end
#...
end
Gee. Once again I want my module methods inherited.
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.
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
Hi --
Is there any reason not to just do that as:
(class << c; self; end).class_eval
BTW, There's an RCP asking for this.
I know that a lot of people would really like this (myself included).
Has anyone voiced any serious reasons why *not* to make this the
default behavior?
Hi --
I guess that means you don't think the reasons given in the comment
section of the RCR are serious
I don't think it's a good idea, because I see no reason to favor the
hypothesis that a module's singleton methods are appropriate for a
class, just because the module's instance methods are appropriate for
instances of the class. That's one possible scenario (and it can
easily be done, already, though I think it's not necessarily a sign of
optimal design), but I don't see why it should be the starting point
and everything else a deviation from it. See the RCR site for more
comments from me and others.
class B
end
module M
def M.new() B.new end
end
class C
include M
end
not least of which.
hypothesis that a module's singleton methods are appropriate
for a class
I wonder if there's a way to make several groups of people happy by
allowing this:
module Z
def method; end
end
class A < Z
end
..which would let modules act as abstract classes if necessary.
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.