# Dynamic methods

**URL:** <https://www.ruby-forum.com/t/dynamic-methods/112884>\
**Category:** Ruby\
**Created:** [August 24, 2007, 3:46am UTC](https://www.ruby-forum.com/t/dynamic-methods/112884 "2007-08-24T03:46:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [August 24, 2007, 3:46am UTC](https://www.ruby-forum.com/t/dynamic-methods/112884/1 "2007-08-24T03:46:37Z")

</div>

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

class Example  
def initialize(…)  
#…  
end

def method\_missing(item)  
call = item.id2name  
process(call)  
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.

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [August 24, 2007, 6:29am UTC](https://www.ruby-forum.com/t/dynamic-methods/112884/2 "2007-08-24T06:29:40Z")

</div>

On Aug 23, 6:46 pm, -j B- [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> call = item.id2name
> process(call)
> 
> ```
> 
> end

def method\_missing(item, \*args, &blk)  
process(item, \*args, &blk) # item is already a symbol  
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.

class Example  
end

foo = “bar”

Example.class\_eval %{  
def #{foo}  
“#{foo}”  
end  
}

Example.new.foo #=\> “bar”

T.

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [August 24, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/dynamic-methods/112884/3 "2007-08-24T12:56:34Z")

</div>

Trans wrote:

> ```
> "#{foo}"
> end
> 
> ```
> 
> }

It’s better not to use string evals:

Example.class\_eval do  
define\_method foo do  
“#{foo}”  
end  
end

lopex

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [August 24, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/dynamic-methods/112884/4 "2007-08-24T13:24:39Z")

</div>

On 8/24/07, Marcin Miel¿yñski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Example.class\_eval %{  
> > “#{foo}”  
> > end  
> > end
> 
> lopex

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  
class \<\< self  
def value; 1120 end # just to show that Ara’s method works  
end  
def value; 101010 end # the value we really want  
end

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

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

class A  
class \<\< self  
alias\_method :old\_new, :new  
def new \*args, &block  
o = old\_new( \*args, &block )  
o.instance\_eval do  
def ara; value end  
end  
o  
end  
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

---

<div class="post-metadata">

**Author:** ![S-jb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@S-jb](https://www.ruby-forum.com/u/S-jb)\
**Post date:** [August 24, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/dynamic-methods/112884/5 "2007-08-24T13:37:41Z")

</div>

On 8/24/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> 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

the above really su…, sorry

class A  
alias\_method :old\_init, :initialize  
def initialize \*args, &blk  
old\_init( \*args, &blk )  
instance\_eval do  
def ara; value end  
end  
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
