# Extend keyword in class definition

**URL:** <https://rubytalk.org/t/extend-keyword-in-class-definition/49369>\
**Category:** ruby-talk\
**Created:** [30 September 2008 09:24 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369 "2008-09-30T09:24:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruud](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@ruud](https://rubytalk.org/u/ruud)\
**Post date:** [30 September 2008 09:24 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/1 "2008-09-30T09:24:27Z")

</div>

Hi list,

I am reading the book 'design patterns in ruby'. On page 201 I find  
this code example:

require 'forwardable'

class WriterDecorator  
&nbsp;&nbsp;&nbsp;&nbsp;extend Forwardable

&nbsp;&nbsp;&nbsp;&nbsp;def\_delegators :@real\_writer, :write\_line, :rewind, :pos, :close

&nbsp;&nbsp;&nbsp;&nbsp;def initialize (real\_writer)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@real\_writer = real\_writer  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

I am a bit surprised by the 'extend Forwardable' line. Can anybody  
explain me what this is? Is it the same as 'include Forwardable' or is  
it the same as 'class WriterDecorator \< Forwardable'?

thanks in advance, Ruud

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [30 September 2008 09:31 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/2 "2008-09-30T09:31:17Z")

</div>

> I am a bit surprised by the 'extend Forwardable' line. Can anybody  
> explain me what this is? Is it the same as 'include Forwardable' or is  
> it the same as 'class WriterDecorator \< Forwardable'?

It's neither. extend() is method that all objects have (classes are objects  
too). It takes the methods from a module (in this case Forwardable) and adds  
them as singleton methods to the object. Forwardable contains the method  
'def\_delegators', which we see here has been added as a singleton method to  
WriterDecorator.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [30 September 2008 09:32 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/3 "2008-09-30T09:32:39Z")

</div>

ruud grosmann wrote:

> I am a bit surprised by the 'extend Forwardable' line. Can anybody  
> explain me what this is? Is it the same as 'include Forwardable' or is  
> it the same as 'class WriterDecorator \< Forwardable'?

No, it's the same as WriterDecorator.extend Forwardable or  
class \<\<WriterDecorator  
&nbsp;&nbsp;include Forwardable  
end  
I.e. the module gets included in the singelton class of WriterDecorator, not  
in WriterDecorator itself (or in other words: the module's methods get added  
as singleton methods of the class instead of as instance methods)

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 September 2008 09:32 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/4 "2008-09-30T09:32:59Z")

</div>

Hi --

> Hi list,
> 
> I am reading the book 'design patterns in ruby'. On page 201 I find  
> this code example:
> 
> require 'forwardable'
> 
> class WriterDecorator  
> &nbsp;&nbsp;&nbsp;extend Forwardable
> 
> &nbsp;&nbsp;&nbsp;def\_delegators :@real\_writer, :write\_line, :rewind, :pos, :close
> 
> &nbsp;&nbsp;&nbsp;def initialize (real\_writer)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@real\_writer = real\_writer  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> I am a bit surprised by the 'extend Forwardable' line. Can anybody  
> explain me what this is? Is it the same as 'include Forwardable' or is  
> it the same as 'class WriterDecorator \< Forwardable'?

It's like a per-object include operation.

&nbsp;&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def speak  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;obj = Object.new  
&nbsp;&nbsp;&nbsp;obj.extend(M)  
&nbsp;&nbsp;&nbsp;obj.speak # "Hi!"

The example you've got extends the class object WriterDecorator. It's  
like this:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extend M  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;C.speak # "Hi!"

David

> **···**
>
> On Tue, 30 Sep 2008, ruud grosmann wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![ruud](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@ruud](https://rubytalk.org/u/ruud)\
**Post date:** [30 September 2008 09:57 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/5 "2008-09-30T09:57:14Z")

</div>

> It's neither. extend() is method that all objects have (classes are objects  
> too). It takes the methods from a module (in this case Forwardable) and adds  
> them as singleton methods to the object. Forwardable contains the method  
> 'def\_delegators', which we see here has been added as a singleton method to  
> WriterDecorator.

thank you for your replies. But I'm afraid that I need some more  
explanation. Isn't a singleton method a method that applies to the  
singleton class of one object? And in this case we're talking about a  
class, not an object?

As such, Forwardable is mixed in, is my first thought. What have I  
understood wrongly?

regards, RUud

---

<div class="post-metadata">

**Author:** ![janus](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@janus](https://rubytalk.org/u/janus)\
**Post date:** [30 September 2008 10:49 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/6 "2008-09-30T10:49:06Z")

</div>

David

Am a little confused by your codes, from all I understood in these  
threads.  
&nbsp;&nbsp;&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def speak  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;obj = Object.new  
&nbsp;&nbsp;&nbsp;&nbsp;obj.extend(M)  
&nbsp;&nbsp;&nbsp;&nbsp;obj.speak

obj.speak should be Object.speak. Why is yours different? And your  
second code agreed with singleton idiom.

Janus

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 September 2008 10:23 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/7 "2008-09-30T10:23:11Z")

</div>

Hi --

> > It's neither. extend() is method that all objects have (classes are objects  
> > too). It takes the methods from a module (in this case Forwardable) and adds  
> > them as singleton methods to the object. Forwardable contains the method  
> > 'def\_delegators', which we see here has been added as a singleton method to  
> > WriterDecorator.
> 
> thank you for your replies. But I'm afraid that I need some more  
> explanation. Isn't a singleton method a method that applies to the  
> singleton class of one object? And in this case we're talking about a  
> class, not an object?
> 
> As such, Forwardable is mixed in, is my first thought. What have I  
> understood wrongly?

Classes \*are\* objects.

&nbsp;&nbsp;&nbsp;c = Class.new # c is an object whose class is Class  
&nbsp;&nbsp;&nbsp;c.extend(SomeModule)

Every class is an instance of the class Class, so you can extend  
classes just like other objects.

I've always said that the answer to 75% of all questions about Ruby  
is: Classes are objects 🙂 It's definitely a very key thing.

David

> **···**
>
> On Tue, 30 Sep 2008, ruud grosmann wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [30 September 2008 10:28 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/8 "2008-09-30T10:28:07Z")

</div>

ruud grosmann wrote:

> Isn't a singleton method a method that applies to the  
> singleton class of one object?

It is a method that is defined on the singleton class of an object, yes.  
As a singleton class by definition only has one instance that means that a  
singleton method is a method that can be called on a single object and that  
object only.

> And in this case we're talking about a  
> class, not an object?

A class is an object:

> > String.is\_a? Class

=\> true

> > String.is\_a? Object

=\> true

> > String.object\_id

=\> -605959678

> As such, Forwardable is mixed in, is my first thought.

It is mixed in. It's mixed into WriterDecorator's singleton class.

> What have I understood wrongly?

I'm not sure. Maybe you mistakenly assumed that a class is its own singleton  
class?

HTH,  
Sebastian

> **···**
>
> --  
> NP: Depeche Mode - Lilian  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 September 2008 10:53 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/9 "2008-09-30T10:53:00Z")

</div>

Hi --

> **···**
>
> On Tue, 30 Sep 2008, janus wrote:
> 
> > David
> > 
> > Am a little confused by your codes, from all I understood in these  
> > threads.  
> > &nbsp;&nbsp;&nbsp;module M  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def speak  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi!"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;obj = Object.new  
> > &nbsp;&nbsp;&nbsp;obj.extend(M)  
> > &nbsp;&nbsp;&nbsp;obj.speak
> > 
> > obj.speak should be Object.speak. Why is yours different? And your  
> > second code agreed with singleton idiom.
> 
> I'm not sure what you mean by "should be". The way it's written,  
> Object does not have a "speak" method, but obj does. That's the effect  
> of extend: it adds module-based functionality to a single object.
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 September 2008 10:37 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/10 "2008-09-30T10:37:00Z")

</div>

Hi --

> ruud grosmann wrote:
> 
> > Isn't a singleton method a method that applies to the  
> > singleton class of one object?
> 
> It is a method that is defined on the singleton class of an object, yes.  
> As a singleton class by definition only has one instance that means that a  
> singleton method is a method that can be called on a single object and that  
> object only.

Although:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;class D \< C  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def C.x  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;D.x # subclass can call superclass's singleton methods

Technically, it's also true that if you extend an object with a  
module, you're not really writing singleton methods, since the methods  
in the module are available to other objects that use the module. The  
singleton\_method\_added callback won't get called, for example.

David

> **···**
>
> On Tue, 30 Sep 2008, Sebastian Hungerecker wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![janus](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@janus](https://rubytalk.org/u/janus)\
**Post date:** [30 September 2008 12:59 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/11 "2008-09-30T12:59:02Z")

</div>

David,

Oh! It is clear now to me.

Janus

---

<div class="post-metadata">

**Author:** ![ruud](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@ruud](https://rubytalk.org/u/ruud)\
**Post date:** [30 September 2008 11:01 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/12 "2008-09-30T11:01:45Z")

</div>

OK. I think now I get what you mean.

To make sure I understood it right:  
my mistake was that there are both singleton methods on classes and  
singleton methods on instances of a class. Essentially the same,  
because classes are objects too. In this case, we're talking about  
singleton methods on the class WriterDecorator. This is done because  
the Forwardable method 'def\_delegators' is used by the class object  
only, not by it's instances. Which would have been the case if  
Forwardable was mixed in.  
Please correct me if I'm wrong.

Thanks anyway for the time spent on helping me on this!

regards, Ruud

> **···**
>
> On 30/09/2008, David A. Black \<dblack@rubypal.com\> wrote:
> 
> > Hi --
> > 
> > On Tue, 30 Sep 2008, Sebastian Hungerecker wrote:
> > 
> > > ruud grosmann wrote:
> > > 
> > > > Isn't a singleton method a method that applies to the  
> > > > singleton class of one object?
> > > 
> > > It is a method that is defined on the singleton class of an object, yes.  
> > > As a singleton class by definition only has one instance that means that a  
> > > singleton method is a method that can be called on a single object and  
> > > that  
> > > object only.
> > 
> > Although:
> > 
> > &nbsp;&nbsp;&nbsp;class C  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;class D \< C  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;def C.x  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;D.x # subclass can call superclass's singleton methods
> > 
> > Technically, it's also true that if you extend an object with a  
> > module, you're not really writing singleton methods, since the methods  
> > in the module are available to other objects that use the module. The  
> > singleton\_method\_added callback won't get called, for example.
> > 
> > David
> > 
> > --  
> > Rails training from David A. Black and Ruby Power and Light:  
> > &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> > &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> > &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> > See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 September 2008 11:05 UTC](https://rubytalk.org/t/extend-keyword-in-class-definition/49369/13 "2008-09-30T11:05:08Z")

</div>

Hi --

> **···**
>
> On Tue, 30 Sep 2008, ruud grosmann wrote:
> 
> > OK. I think now I get what you mean.
> > 
> > To make sure I understood it right:  
> > my mistake was that there are both singleton methods on classes and  
> > singleton methods on instances of a class. Essentially the same,  
> > because classes are objects too. In this case, we're talking about  
> > singleton methods on the class WriterDecorator. This is done because  
> > the Forwardable method 'def\_delegators' is used by the class object  
> > only, not by it's instances. Which would have been the case if  
> > Forwardable was mixed in.  
> > Please correct me if I'm wrong.
> 
> The only thing I'd add is that it's not really accurate to describe  
> methods defined in a module as "singleton methods" of an object that  
> extends itself with the module. A singleton method is really a method  
> defined in the object's singleton class itself.
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
