# Module that includes both class and instance methods?

**URL:** <https://www.ruby-forum.com/t/module-that-includes-both-class-and-instance-methods/86023>\
**Category:** Ruby\
**Created:** [February 19, 2007, 6:21am UTC](https://www.ruby-forum.com/t/module-that-includes-both-class-and-instance-methods/86023 "2007-02-19T06:21:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 19, 2007, 6:21am UTC](https://www.ruby-forum.com/t/module-that-includes-both-class-and-instance-methods/86023/1 "2007-02-19T06:21:47Z")

</div>

Hello,

I’m trying to create a module that will have class and instance methods  
that are made available to any object that includes it. I can do the  
job with two lines of code but would prefer to do it with one line of  
code if possible (to make sure no one tries to foolishly use only one  
half of the functionality - it should be all or nothing). Here’s what I  
have so far:

[code starts]  
module Models  
module Ver  
def Ver.is\_versioned(object)  
include Models::VersionsTables::InstanceMethods  
object.extend Models::VersionsTables::ClassMethods  
end  
end  
module VersionsTables  
module InstanceMethods  
def instance\_test  
“instance test”  
end  
end  
module ClassMethods  
def class\_test  
“class\_test”  
end  
end  
end  
end

class Live  
Models::Ver::is\_versioned(self)  
#include Models::VersionsTables::InstanceMethods  
#extend Models::VersionsTables::ClassMethods  
end  
puts “::”+Live.class\_test  
live = Live.new  
puts live.instance\_test

[code ends]

My code can get the class methods fine - “object.extend” works like a  
charm: I can’t figure out what the equivalent is for an instance  
method.

Or perhaps there’s a wonderful way to do this using entirely different  
syntax? I’d love to be able to say: “extend Modules::Ver” or something  
like that and have it work - but it seems that no code gets executed  
that’s left open in the Ver module when that module is included or  
extended?

Any thoughts or ideas?

Thank you for any help!

## Steve

The law is an envious monster.

- Rex Stout

---

<div class="post-metadata">

**Author:** ![sjmadsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@sjmadsen](https://www.ruby-forum.com/u/sjmadsen)\
**Post date:** [February 19, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/module-that-includes-both-class-and-instance-methods/86023/2 "2007-02-19T15:42:55Z")

</div>

On 2/19/07, Steve M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,

> I’m trying to create a module that will have class and instance methods  
> that are made available to any object that includes it. I can do the  
> job with two lines of code but would prefer to do it with one line of  
> code if possible (to make sure no one tries to foolishly use only one  
> half of the functionality - it should be all or nothing). Here’s what I  
> have so far:

Please dig through the archives for this. It has been discussed many  
different times with at least a half dozen different solutions.

And at least as many people (myself included) wondering why one would  
ever want to consider tightly coupling something like this.

-austin
