# Dynamic methods

**URL:** <https://rubytalk.org/t/dynamic-methods/40159>\
**Category:** ruby-talk\
**Created:** [24 August 2007 01:46 UTC](https://rubytalk.org/t/dynamic-methods/40159 "2007-08-24T01:46:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![j\_B](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@j\_B](https://rubytalk.org/u/j_B)\
**Post date:** [24 August 2007 01:46 UTC](https://rubytalk.org/t/dynamic-methods/40159/1 "2007-08-24T01:46:43Z")

</div>

I have seen examples of catching unkown methods called on a class with  
method\_missing.  
For example:

class Example  
&nbsp;&nbsp;&nbsp;def initialize(...)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def method\_missing(item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call = item.id2name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;process(call)  
&nbsp;&nbsp;&nbsp;end  
end

But I'm not sure if that is an 'acceptable' way to do things. Also,  
suppose that I wanted to add a method dynamically that takes arguments,  
is this even possible?  
My general thought is allowing functionality to be added to a program  
dynamically based on a set of rules in a config file.  
Any suggestions are welcome.  
Thank you.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [24 August 2007 04:28 UTC](https://rubytalk.org/t/dynamic-methods/40159/2 "2007-08-24T04:28:57Z")

</div>

> I have seen examples of catching unkown methods called on a class with  
> method\_missing.  
> For example:
> 
> class Example  
> &nbsp;&nbsp;&nbsp;def initialize(...)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#...  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def method\_missing(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call = item.id2name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;process(call)  
> &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(item, \*args, &blk)  
&nbsp;&nbsp;&nbsp;&nbsp;process(item, \*args, &blk) # item is already a symbol  
&nbsp;&nbsp;end

> end
> 
> But I'm not sure if that is an 'acceptable' way to do things. Also,  
> suppose that I wanted to add a method dynamically that takes arguments,  
> is this even possible?  
> My general thought is allowing functionality to be added to a program  
> dynamically based on a set of rules in a config file.  
> Any suggestions are welcome.  
> Thank you.

Also, keep in mind Ruby classes are open. You can add real methods to  
them on the fly.

&nbsp;&nbsp;class Example  
&nbsp;&nbsp;end

&nbsp;&nbsp;foo = "bar"

&nbsp;&nbsp;Example.class\_eval %{  
&nbsp;&nbsp;&nbsp;&nbsp;def #{foo}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{foo}"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}

&nbsp;&nbsp;Example.new.foo #=\> "bar"

T.

> **···**
>
> On Aug 23, 6:46 pm, -j B- \<jrb...@drexel.edu\> wrote:

---

<div class="post-metadata">

**Author:** ![Marcin\_Miel\_ynski](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Marcin\_Miel\_ynski](https://rubytalk.org/u/Marcin_Miel_ynski)\
**Post date:** [24 August 2007 10:55 UTC](https://rubytalk.org/t/dynamic-methods/40159/3 "2007-08-24T10:55:07Z")

</div>

Trans wrote:

> Also, keep in mind Ruby classes are open. You can add real methods to  
> them on the fly.
> 
> &nbsp;&nbsp;class Example  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;foo = "bar"
> 
> &nbsp;&nbsp;Example.class\_eval %{  
> &nbsp;&nbsp;&nbsp;&nbsp;def #{foo}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{foo}"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}

It's better not to use string evals:

Example.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method foo do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{foo}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

lopex

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 August 2007 11:23 UTC](https://rubytalk.org/t/dynamic-methods/40159/4 "2007-08-24T11:23:05Z")

</div>

You are right for sure here Marcin, however are you aware that the two  
are not exactly the same? In case you get bitten by define\_method  
maybe Ara's recent post about instance\_eval{ def ... } might be  
helpful,  
the following is a "String-eval-free" way to define a method on a  
class \*without\* being exposed to the closure of the current  
environment.  
----------------------- 8\< ----------------------

value = 222 ## evil closure variable shadowing A#value

class A  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;def value; 1120 end # just to show that Ara's method works  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def value; 101010 end # the value we really want  
end

A.class\_eval %{  
&nbsp;&nbsp;def tom; value end  
} ## works but we use the threaded String eval ☹

A.class\_eval do  
&nbsp;&nbsp;define\_method :marcin do value end # value is the closure of course ☹  
end ## nice, but does not work ☹

class A  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;alias\_method :old\_new, :new  
&nbsp;&nbsp;&nbsp;&nbsp;def new \*args, &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o = old\_new( \*args, &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ara; value end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end ## this does the trick, simple concise, trivial ☹ well we wish it were.  
## and there are issues with the alias it is a potential redefeinition disaster

a = A.new  
p [:tom, a.tom]  
p [:marcin, a.marcin]  
p [:ara, a.ara]

Cheers  
Robert

> **···**
>
> On 8/24/07, Marcin Mielżyński \<lopx@gazeta.pl\> wrote:
> 
> > Trans wrote:
> > 
> > \> Also, keep in mind Ruby classes are open. You can add real methods to  
> > \> them on the fly.  
> > \>  
> > \> class Example  
> > \> end  
> > \>  
> > \> foo = "bar"  
> > \>  
> > \> Example.class\_eval %{  
> > \> def #{foo}  
> > \> "#{foo}"  
> > \> end  
> > \> }  
> > \>
> > 
> > It's better not to use string evals:
> > 
> > Example.class\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method foo do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{foo}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > lopex
> 
> --  
> I'm an atheist and that's it. I believe there's nothing we can know  
> except that we should be kind to each other and do what we can for  
> other people.  
> -- Katharine Hepburn

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 August 2007 11:37 UTC](https://rubytalk.org/t/dynamic-methods/40159/5 "2007-08-24T11:37:07Z")

</div>

the above really su..., sorry

class A  
&nbsp;&nbsp;alias\_method :old\_init, :initialize  
&nbsp;&nbsp;def initialize \*args, &blk  
&nbsp;&nbsp;&nbsp;&nbsp;old\_init( \*args, &blk )  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ara; value end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
# the issues remain though, we could apply the method redefintion  
hammer Pit and Ara came up with and just redefine initialize like  
that, but that would make it about 100 lines I guess ☹

Robert

> **···**
>
> On 8/24/07, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > class A  
> > &nbsp;&nbsp;class \<\< self  
> > &nbsp;&nbsp;&nbsp;&nbsp;alias\_method :old\_new, :new  
> > &nbsp;&nbsp;&nbsp;&nbsp;def new \*args, &block  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o = old\_new( \*args, &block )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.instance\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ara; value end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end ## this does the trick, simple concise, trivial ☹ well we wish it were.  
> > ## and there are issues with the alias it is a potential redefeinition disaster
> 
> --  
> I'm an atheist and that's it. I believe there's nothing we can know  
> except that we should be kind to each other and do what we can for  
> other people.  
> -- Katharine Hepburn
