# Sending extend and include to your own class definition

**URL:** <https://www.ruby-forum.com/t/sending-extend-and-include-to-your-own-class-definition/168613>\
**Category:** Ruby\
**Created:** [May 22, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/sending-extend-and-include-to-your-own-class-definition/168613 "2009-05-22T21:43:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@rlf](https://www.ruby-forum.com/u/rlf)\
**Post date:** [May 22, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/sending-extend-and-include-to-your-own-class-definition/168613/1 "2009-05-22T21:43:11Z")

</div>

hello.

I’m trying to learn the ruby way of doing things, and ran across some  
code that I’m finding hard to understand. It seems to be sending  
extend and include messages of inner modules to the class def itself.

## I’ve boiled the sample down to something like this.

class C

module ClassMethods  
def c1  
‘Class method c1’  
end

```
def c2
  'Class method c2'
end

```

end

send :extend, ClassMethods

module InstanceMethods  
def i1  
‘Instance method i1’  
end

```
def i2
  'Instance method i2'
end

```

end

send :include, InstanceMethods

end

* * *

I think I must be missing something.

Isn’t the receiver of the send message the Class itself? If not, who  
is the receiver?

If it is, what does this technique offer compared to just declaring  
the class and instance methods in the first place?

I’d really appreciate some clarification about this style.

TIA.

---

<div class="post-metadata">

**Author:** ![rlf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@rlf](https://www.ruby-forum.com/u/rlf)\
**Post date:** [May 22, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/sending-extend-and-include-to-your-own-class-definition/168613/2 "2009-05-22T23:02:53Z")

</div>

On May 22, 2009, at 3:40 PM, rlf wrote:

> I’m trying to learn the ruby way of doing things, and ran across some  
> code that I’m finding hard to understand. It seems to be sending  
> extend and include messages of inner modules to the class def itself.

> send :extend, ClassMethods  
> […]  
> send :include, InstanceMethods

These could be more simply written as:  
extend ClassMethods  
include InstanceMethods

> Isn’t the receiver of the send message the Class itself? If not, who  
> is the receiver?

Yes, the class ‘C’ is the receiver.

> If it is, what does this technique offer compared to just declaring  
> the class and instance methods in the first place?

Perhaps because the modules are going to be used elsewhere? If you  
declare the methods directly as class and instance methods it is  
awkward to incorporate them into other classes or modules via extend  
and include.
