# Can class methods be in a module?

**URL:** <https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831>\
**Category:** Ruby\
**Created:** [October 21, 2007, 4:16am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831 "2007-10-21T04:16:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 4:16am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/1 "2007-10-21T04:16:59Z")

</div>

Is it possible to put a class method in a module, for example:

module FtpClientMod  
def self.do\_send(file\_template, show\_progress)

and include it in a class as follows?

require ‘ftpclientmod’

class FtpClient  
include FtpClientMod

When I try this I get an error that the method cannot be found.

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 4:32am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/2 "2007-10-21T04:32:46Z")

</div>

On 10/21/07, Bazsl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> include FtpClientMod
> 
> When I try this I get an error that the method cannot be found.

[http://www.ruby-doc.org/core/classes/Object.html#M000337](http://www.ruby-doc.org/core/classes/Object.html#M000337)

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 5:19am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/3 "2007-10-21T05:19:02Z")

</div>

Michael F. wrote:

> >

I guess I was not clear. I am trying to place a class method in a module  
and include it in the class. I am not trying to instantiate an instance  
of a class and and include the method from the module in the instance,  
which, if I am reading your reference correctly, is what extend does.  
Note that I am very new to Ruby so I may be missing something. Thanks.

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 8:57am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/4 "2007-10-21T08:57:54Z")

</div>

Bazsl wrote:

> Is it possible to put a class method in a module, for example:
> 
> module FtpClientMod  
> def self.do\_send(file\_template, show\_progress)
> 
> and include it in a class as follows?
> 
> require ‘ftpclientmod’
> 
> class FtpClient  
> include FtpClientMod
> 
> When I try this I get an error that the method cannot be found.

Maybe this will work for you:

module MyMethods  
def greet  
puts “hello”  
end  
end

class Dog  
include MyMethods  
extend MyMethods  
end

Dog.greet #hello

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 11:06am UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/5 "2007-10-21T11:06:12Z")

</div>

module FtpClientMod  
def do\_send(file\_template, show\_progress)  
puts “do\_send #{file\_template}”  
end  
end

class FtpClient  
class \<\< self  
include FtpClientMod  
end  
end

FtpClient.do\_send(‘foo’, 0)

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/6 "2007-10-21T17:40:35Z")

</div>

tho\_mica\_l wrote:

> end
> 
> FtpClient.do\_send(‘foo’, 0)

Thanks. That works but the method in the module does not have access to  
class variables. Is there any way to give the method in the module  
access to class variables?

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 21, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/7 "2007-10-21T17:39:24Z")

</div>

7stud – wrote:

> > 
> 
> def greet  
> Dog.greet #hello

That works but the method in the module does not have access to class  
variables. Is there any way to give the method in the module access to  
class variables?

---

<div class="post-metadata">

**Author:** ![Bazsl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ad7895/32.png) [@Bazsl](https://www.ruby-forum.com/u/Bazsl)\
**Post date:** [October 26, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/can-class-methods-be-in-a-module/117831/8 "2007-10-26T22:20:55Z")

</div>

> Thanks. That works but the method in the module does not have access to  
> class variables. Is there any way to give the method in the module  
> access to class variables?

Which class variable do you mean?

module FtpClientMod  
@foo = 1  
@@bar = 1  
def do\_send(file\_template, show\_progress)  
puts “do\_send #{file\_template} #@foo #@@bar #{gimme\_bar}”  
end  
def gimme\_bar  
@@bar  
end  
end

class FtpClient  
@foo = 2  
@@bar = 2  
class \<\< self  
@foo = 3  
include FtpClientMod

```
    def gimme_bar
        @@bar
    end
end

```

end

FtpClient.do\_send(‘foo’, 0)  
=\> do\_send foo 2 1 2
