# Class methods and modules

**URL:** <https://www.ruby-forum.com/t/class-methods-and-modules/109338>\
**Category:** Ruby\
**Created:** [July 13, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/class-methods-and-modules/109338 "2007-07-13T16:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [July 13, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/class-methods-and-modules/109338/1 "2007-07-13T16:19:49Z")

</div>

Hello

I wan’t to design a Module that defines some methods that I wan’t as  
class methods in the classes that includes them. Is it possible?

Thanks

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [July 13, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/class-methods-and-modules/109338/2 "2007-07-13T16:43:48Z")

</div>

On 7/13/07, Pedro C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello
> 
> I wan’t to design a Module that defines some methods that I wan’t as  
> class methods in the classes that includes them. Is it possible?
> 
> Thanks

module MyModule  
module ClassMethods  
def class\_method\_one( )  
puts “you’re in class method one”  
end

```
def class_method_two( )
  puts "you're in class method two"
end

```

end # module ClassMethods

def self.included( other )  
other.extend ClassMethods if Class === other  
end

def method\_one( )  
puts “you’re in method one”  
end  
end

class C  
include MyModule  
end

C.new.method\_one #=\> you’re in method one  
C.class\_method\_one #=\> you’re in class method one  
C.class\_method\_two #=\> you’re in class method two

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [July 13, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/class-methods-and-modules/109338/3 "2007-07-13T17:04:21Z")

</div>

Hi. Thanks!

I found another way.

Module MyModule  
def m  
end  
end

class MyClass  
extend MyDodule  
end

MyClass.m

Can anyone explain the “job” of extend?

Thanks.

---

<div class="post-metadata">

**Author:** ![jotape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jotape](https://www.ruby-forum.com/u/jotape)\
**Post date:** [July 13, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/class-methods-and-modules/109338/4 "2007-07-13T17:17:11Z")

</div>

Pedro C. wrote:

> Hi. Thanks!
> 
> I found another way.
> 
> Module MyModule  
> def m  
> end  
> end
> 
> class MyClass  
> extend MyDodule  
> end
> 
> MyClass.m
> 
> Can anyone explain the “job” of extend?
> 
> Thanks.

Extend includes the methods in the singleton class. E.g.  
MyClass.extend(MyModue) is (almost) equivalent to:  
class \<\<MyClass  
include MyModule  
end  
Only almost because iirc a different callback is used.

Regards  
Stefan
