How Extend AR Instance Methods?

I’m trying to extend AR with a method which needs to be available to
both the class and the instance.

I’ve tried a gajillion permutations based on examples. I can get the
method added to the Class, but not to instances.

This seems like a logical pattern to me, but it’s apparently incorect.

module SpiffyMethods

module MyMethods
def shared_method
return “Hello?”
end
end

def self.included(base)
base.extend ClassMethods # works
base.include InstanceMethods # fails
end

module ClassMethods
include MyMethods
end

module InstanceMethods
include MyMethods
end
end

Need a point in the right direction. Thx.

– gw

On Friday 15 October 2010, Greg W. wrote:

I’m trying to extend AR with a method which needs to be available to
both the class and the instance.

I’ve tried a gajillion permutations based on examples. I can get the
method added to the Class, but not to instances.

module MyMethods
def self.included(base)
base.extend(self)
end

def shared_method
“Hello?”
end
end

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

Well, that was easy. :stuck_out_tongue:

Thanks!

– gw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs