The most straightforward approach that satisfies the thread subject
would be to use #extend.
This was already covered in the initial post, he's using a module with
"self.foo", which doesn't work with extend since that method remains
with the module rather than transferring to the class.
Aargh! Overlooked that. Posting after midnight... Thank you for the
heads up, Joel!
My 2 Cents: I do not think it is a good idea to do what OP is trying
to do. After all methods defined on the module itself affect this
exact module's state. If one wants to provide some kind of
functionality that should be reusable by class instances then defining
a module with instance methods and extending the class would be the
proper way.
If there needs to be related behavior, i.e. instance methods using
class methods then I would use two modules. You can either act on the
extend event or on the #included event. I'd probably use the #included callback as that stresses the fact that I want to provide
instance functionality.
#!/usr/bin/ruby -w
module Foo
module Foo4Class
def instance_count @instance_count
end
end
def self.included(cl)
cl.extend Foo4Class
cl.instance_variable_set '@instance_count', 0
cl.singleton_class.class_eval do
def new(*a,&b)
super.tap { @instance_count += 1 }
end
end
end
def shout_instance_count
printf "There have been %d of us created!\n", self.class.instance_count
end
end
On Tue, Oct 29, 2013 at 9:36 AM, Joel Pearson <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1125970:
The most straightforward approach that satisfies the thread subject
would be to use #extend.
This was already covered in the initial post, he's using a module with
"self.foo", which doesn't work with extend since that method remains
with the module rather than transferring to the class.
I have updated the gist with a more appropriate solution that reduces
meta programming in favor of ordinary methods.
Cheers
robert
···
On Tue, Oct 29, 2013 at 12:56 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Tue, Oct 29, 2013 at 9:36 AM, Joel Pearson <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1125970:
The most straightforward approach that satisfies the thread subject
would be to use #extend.
This was already covered in the initial post, he's using a module with
"self.foo", which doesn't work with extend since that method remains
with the module rather than transferring to the class.
Aargh! Overlooked that. Posting after midnight... Thank you for the
heads up, Joel!
My 2 Cents: I do not think it is a good idea to do what OP is trying
to do. After all methods defined on the module itself affect this
exact module's state. If one wants to provide some kind of
functionality that should be reusable by class instances then defining
a module with instance methods and extending the class would be the
proper way.
If there needs to be related behavior, i.e. instance methods using
class methods then I would use two modules. You can either act on the
extend event or on the #included event. I'd probably use the #included callback as that stresses the fact that I want to provide
instance functionality.