# How can I use module methods as class methods?

**URL:** <https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175>\
**Category:** Ruby\
**Created:** [October 28, 2013, 3:37pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175 "2013-10-28T15:37:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 28, 2013, 3:37pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/1 "2013-10-28T15:37:01Z")

</div>

Hi,

Here is a code :

module A  
def self.foo  
1  
end  
def bar  
2  
end  
end

## Now if I do extend

class B  
extend A  
end

B.bar # 2  
B.foo # error  
B.new.foo # error

## Now if I do include

class B  
include A  
end

B.new.bar # 2  
B.foo # error  
B.new.foo # error

Any chance to get access or do like `B.foo` ?

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 28, 2013, 3:47pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/2 "2013-10-28T15:47:17Z")

</div>

You should be able to do that by adapting this approach:

> **[SoftwareByJosh.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=softwarebyjosh.com)**
>
> Premium domains add authority to your site. Transparent pricing. 1 year WHOIS privacy included. 30-day money back guarantee.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 28, 2013, 4:16pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/3 "2013-10-28T16:16:44Z")

</div>

When you want to include Modules into a Class you would typically do

require filename # if you are using two different script

class B  
include A # =\> includes all methods in the Module  
end

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 28, 2013, 4:25pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/4 "2013-10-28T16:25:15Z")

</div>

John M. wrote in post #1125888:

> When you want to include Modules into a Class you would typically do
> 
> require filename # if you are using two different script
> 
> class B  
> include A # =\> includes all methods in the Module  
> end

No it wouldn’t :

module A  
def self.foo  
1  
end  
def bar  
2  
end  
end

## Now if I do include

class B  
include A  
end

B.new.foo # undefined method `foo' for #<B:0x17148f0> (NoMethodError) B.foo # undefined method `foo’ for B:Class (NoMethodError)

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 28, 2013, 4:31pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/5 "2013-10-28T16:31:46Z")

</div>

By defining self.foo you’re explicitly saying that it belongs to module  
A. If you wanted to include or extend a class with it you wouldn’t use  
“self”.

The way to accomplish this is demonstrated with some metaprogramming  
that I linked to in my previous response.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 29, 2013, 12:09am UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/6 "2013-10-29T00:09:02Z")

</div>

On Mon, Oct 28, 2013 at 4:31 PM, Joel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> By defining self.foo you’re explicitly saying that it belongs to module  
> A. If you wanted to include or extend a class with it you wouldn’t use  
> “self”.
> 
> The way to accomplish this is demonstrated with some metaprogramming  
> that I linked to in my previous response.

The most straightforward approach that satisfies the thread subject  
would be to use #extend.

module M  
def bar; 123 end  
end

class X  
extend M  
end

X.bar # =\> 123

Cheers

robert

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 29, 2013, 9:36am UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/7 "2013-10-29T09:36:35Z")

</div>

Robert K. 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.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 29, 2013, 12:57pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/8 "2013-10-29T12:57:08Z")

</div>

On Tue, Oct 29, 2013 at 9:36 AM, Joel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Robert K. 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.

#!/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

class Bar  
include Foo  
end

Array.new(10) { Bar.new }.each(&:shout\_instance\_count)

> <https://gist.github.com/rklemme/7213313>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [July 15, 2015, 12:58am UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/9 "2015-07-15T00:58:05Z")

</div>

What I did for convenience and easy read is:

module ModIncl  
def whatever  
…  
end  
end  
module Mod  
extend ModIncl  
end

Now if I want to call a method without messing my class I can use  
`Mod.whatever`. If I want to mess the methods into my class to call them  
easily, then I do `include ModIncl`.

Not sure how @Robert K.'s answer made me think about this. I can  
hardly comprehend what he does but thanks anyway.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [October 29, 2013, 1:52pm UTC](https://www.ruby-forum.com/t/how-can-i-use-module-methods-as-class-methods/237175/10 "2013-10-29T13:52:38Z")

</div>

On Tue, Oct 29, 2013 at 12:56 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> heads up, Joel!  
> 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.

> [Example for adding functionality to instances of a class which needs help from class methods of the same class. Use two modules - one for instance methods and one for class methods. · GitHub](https://gist.github.com/rklemme/7213313)

I have updated the gist with a more appropriate solution that reduces  
meta programming in favor of ordinary methods.

Cheers

robert
