# Newbie: how to dynamically add methods?

**URL:** <https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968>\
**Category:** ruby-talk\
**Created:** [16 February 2006 15:13 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968 "2006-02-16T15:13:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BA\_Baracus](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BA\_Baracus](https://rubytalk.org/u/BA_Baracus)\
**Post date:** [16 February 2006 15:13 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/1 "2006-02-16T15:13:43Z")

</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  
&nbsp;&nbsp;...

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

> **···**
>
> --  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your time!

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [16 February 2006 15:49 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/2 "2006-02-16T15:49:06Z")

</div>

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.

> **···**
>
> On 2/16/06, Jeff Cohen \<devlists-ruby-talk@devlists.com\> 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  
> > &nbsp;&nbsp;...
> > 
> > and I'm wondering exactly what's going on.
> > 
> > Anybody know of a good book or online tutorial for this kind of thing?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [16 February 2006 16:56 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/3 "2006-02-16T16:56:00Z")

</div>

Hi --

> 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:

&nbsp;&nbsp;&nbsp;def a.meth  
&nbsp;&nbsp;&nbsp;end

and

&nbsp;&nbsp;&nbsp;class \<\< a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def meth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Fri, 17 Feb 2006, Jeff Cohen wrote:
> 
> --  
> David A. Black (dblack@wobblini.net)  
> 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:** ![Cameron\_McBride](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Cameron\_McBride](https://rubytalk.org/u/Cameron_McBride)\
**Post date:** [16 February 2006 17:07 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/4 "2006-02-16T17:07:41Z")

</div>

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

> **···**
>
> On 2/16/06, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > Except for some minor differences that usually don't matter, these two  
> > things are equivalent:
> > 
> > &nbsp;&nbsp;&nbsp;def a.meth  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > and
> > 
> > &nbsp;&nbsp;&nbsp;class \<\< a  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def meth  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > In both cases, you're defining a method that only the object a can  
> > call.

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [16 February 2006 17:53 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/5 "2006-02-16T17:53:53Z")

</div>

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

> **···**
>
> On 2/16/06, Wilson Bilkovich \<wilsonb@gmail.com\> wrote:
> 
> > On 2/16/06, Jeff Cohen \<devlists-ruby-talk@devlists.com\> 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.  
> > \>  
> > \> 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)
> 
> --  
> R. Mark Volkmann  
> Partner, Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![BA\_Baracus](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BA\_Baracus](https://rubytalk.org/u/BA_Baracus)\
**Post date:** [16 February 2006 21:58 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/6 "2006-02-16T21:58:24Z")

</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.

> **···**
>
> --  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your time!

---

<div class="post-metadata">

**Author:** ![pesachzon](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@pesachzon](https://rubytalk.org/u/pesachzon)\
**Post date:** [17 February 2006 20:08 UTC](https://rubytalk.org/t/newbie-how-to-dynamically-add-methods/24968/7 "2006-02-17T20:08:31Z")

</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
