# Answer: Module that includes both class and instance methods

**URL:** <https://www.ruby-forum.com/t/answer-module-that-includes-both-class-and-instance-methods/86026>\
**Category:** Ruby\
**Created:** [February 19, 2007, 6:34am UTC](https://www.ruby-forum.com/t/answer-module-that-includes-both-class-and-instance-methods/86026 "2007-02-19T06:34: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:34am UTC](https://www.ruby-forum.com/t/answer-module-that-includes-both-class-and-instance-methods/86026/1 "2007-02-19T06:34:47Z")

</div>

Hello,

After an hour of farting around trying to figure this out, of course I  
do figure it out 20 seconds after writing to the list. Well, here’s one  
solution to my issue - I’d still love to see a one-line solution if  
anyone can come up with one (but it’s not necessary for me at all, just  
curiosity and desire to learn cool Rubyisms):

[code starts]

module Models  
module Ver  
def is\_versioned  
include Models::VersionsTables::InstanceMethods  
self.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  
extend Models::Ver

is\_versioned  
end

puts “::”+Live.class\_test  
live = Live.new  
puts live.instance\_test

[code ends]

So it uses two lines of code, but I can live with that because only the  
second line of code actually causes the class AND instance methods to  
be included - so they’re all or nothing - you either get class and  
instance or nothing (the reason I care is that some monkey-see,  
monkey-do coder down the road could pull something into their class out  
of context and I want these functions to either work or fail, not yield  
partial and erroneous capability).

Sorry for being the noise in your signal,

## 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, 6:14pm UTC](https://www.ruby-forum.com/t/answer-module-that-includes-both-class-and-instance-methods/86026/2 "2007-02-19T18:14:20Z")

</div>

On Feb 19, 12:33 am, Steve M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> After an hour of farting around trying to figure this out, of course I  
> do figure it out 20 seconds after writing to the list. Well, here’s one  
> solution to my issue - I’d still love to see a one-line solution if  
> anyone can come up with one (but it’s not necessary for me at all, just  
> curiosity and desire to learn cool Rubyisms):

Hi–

Have a look at facets’ module/class\_extension. You can read the  
conversations that led to it’s development here:

[http://groups.google.com/group/ruby-talk-google/search?hl=en&group=ruby-talk-google&q=class\_extension&qt\_g=Search+this+group](http://groups.google.com/group/ruby-talk-google/search?hl=en&group=ruby-talk-google&q=class_extension&qt_g=Search+this+group)

T.
