# Newbie: how to dynamically add methods?

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907>\
**Category:** Ruby\
**Created:** [February 16, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907 "2006-02-16T16:16:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/1 "2006-02-16T16:16:29Z")

</div>

The PickAxe doesn’t seem to cover details of how to dynamically create  
class or add methods to existing classes. For example, sometimes I see  
people doing things like this:

class \<\< self  
…

and I’m wondering exactly what’s going on.

Anybody know of a good book or online tutorial for this kind of thing?

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/2 "2006-02-16T16:49:40Z")

</div>

On 2/16/06, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

This may help:  
[http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html)

class \<\< self is usually used for things that are a bit beyond a basic  
method definition.  
For example, if you just want to define a new class method called  
blah, you could write:  
def self.blah  
…  
end

However, if you want to do alias\_method on a class method, you can’t do:  
alias\_method :self.blah, etc, etc.  
You need to put it inside a class \<\< self section.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/3 "2006-02-16T18:08:29Z")

</div>

On 2/16/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> end
> 
> In both cases, you’re defining a method that only the object a can  
> call.

It also might be worth pointing out that if ‘a’ is a class, you’ll be  
adding a class method.

so the notation

class \<\< self  
…

is often used within a class to defind a class method. This type of  
usage is described on page 34 in PickAxe 2 (the sidebar thing).

Cameron

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/4 "2006-02-16T18:56:48Z")

</div>

On 2/16/06, Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Anybody know of a good book or online tutorial for this kind of thing?
> 
> This may help:  
> [http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html](http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html)

I wonder if the stiff server is down now. That URL isn’t working, but  
Google knows about it.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/5 "2006-02-16T17:56:22Z")

</div>

Hi –

On Fri, 17 Feb 2006, Jeff C. wrote:

> The PickAxe doesn’t seem to cover details of how to dynamically create  
> class or add methods to existing classes. For example, sometimes I see  
> people doing things like this:
> 
> class \<\< self  
> …
> 
> and I’m wondering exactly what’s going on.

The “\<\< obj” notation takes you into the singleton class of obj, which  
is where that particular object’s singleton methods (i.e., those that  
only that object can call) are stored.

Except for some minor differences that usually don’t matter, these two  
things are equivalent:

def a.meth  
end

and

class \<\< a  
def meth  
end  
end

In both cases, you’re defining a method that only the object a can  
call.

> Anybody know of a good book or online tutorial for this kind of thing?

[http://www.rubygarden.org/ruby?SingletonTutorial](http://www.rubygarden.org/ruby?SingletonTutorial) goes over singleton  
classes and methods, and also the other meaning of “singleton” in  
Ruby (not closely related, except by coincidence of name, but good to  
know about _because_ of the coincidence of name).

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 16, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/6 "2006-02-16T22:58:46Z")

</div>

And how does class\_eval and instance\_eval play a role here?

I think I’m almost getting it but feel like I need one or two more dots  
to connect.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 17, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/newbie-how-to-dynamically-add-methods/53907/7 "2006-02-17T21:08:49Z")

</div>

As far as I remember,  
class\_eval evaluates a block of code within the context of a class. So,  
if you have a code which adds methods, they will be added to the class  
and will be available to all instances of this class.

instance\_eval is evaluated in the context of an object which is an  
instance of some class. If you add methods they will be available only  
to this object.

Cheers

Eugene Vahlis  
Department of Computer Science  
University of Toronto
