# Defining methods dynamically?

**URL:** <https://www.ruby-forum.com/t/defining-methods-dynamically/111040>\
**Category:** Ruby\
**Created:** [August 2, 2007, 3:43am UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040 "2007-08-02T03:43:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 3:43am UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/1 "2007-08-02T03:43:50Z")

</div>

Having looked through the Class and Method rdoc, and the Reflection  
section of Pickaxe, and still not seeing anything (even though it was  
probably right in front of my nose), I’m forced to fall on the mercy of  
strangers and ask…how does one add a method to a class while the  
program is running?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 3:50am UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/2 "2007-08-02T03:50:51Z")

</div>

Hi –

On Thu, 2 Aug 2007, Kenneth McDonald wrote:

> Having looked through the Class and Method rdoc, and the Reflection section  
> of Pickaxe, and still not seeing anything (even though it was probably right  
> in front of my nose), I’m forced to fall on the mercy of strangers and  
> ask…how does one add a method to a class while the program is running?

There’s actually no other way to do it 🙂 But I think what you’re  
looking for is #define\_method, which lets you define methods with  
dynamically-determined names and also gives you a way to flatten the  
variable scope of a method definition.

For example:

str = “greet”  
c = Class.new  
c.class\_eval { define\_method(str) { puts “Hello” } }

c.new.greet # Hello

David

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/3 "2007-08-02T14:07:55Z")

</div>

On Aug 01, 2007, at 21:49 , removed\_email\_address@domain.invalid wrote:

> There's actually no other way to do it :-) But I think what you're looking for is #define\_method, which lets you define methods with dynamically-determined names and also gives you a way to flatten the variable scope of a method definition.

David,

I do not follow what you mean by “flatten the variable scope of a  
method definition”.  
Could you please explain what this means a bit?

Many thanks!

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 4:33am UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/4 "2007-08-02T04:33:53Z")

</div>

fr: Kenneth McDonald [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :

# Having looked through the Class and Method rdoc, and the Reflection

# section of Pickaxe, and still not seeing anything (even though it was

# probably right in front of my nose), I’m forced to fall on

# the mercy of

# strangers and ask…how does one add a method to a class while the

# program is running?

this is just one stupid example,

C:\temp\>cat test.rb  
puts “i’m running…”

class C  
puts “i’m running… inside class”  
end

puts “i’m running…”

c = C.new

puts c.meth rescue puts “no c.meth yet, so this will print”

class C  
puts “i’m running… inside class; creating method meth now”  
def meth  
“yeow!”  
end  
end

puts “i’m running…”  
puts c.meth rescue puts “meth will run now, so this will not print”

puts “i’m running…”  
puts c.meth2 rescue puts “method meth2 not yet defined”

puts “let us put the creation on a string…”  
puts “i’m running…”  
s = \<\<CLASS  
class C  
puts “i’m running… inside class; creating method second meth2 now”  
def meth2  
“hehey!”  
end  
end  
CLASS

puts “i’m running…”  
eval(s)

puts “now meth2 will run”

puts “i’m running…”  
puts c.meth2 rescue puts “meth2 will run, so this will not print”

puts “i’m running…”  
puts c.meth3 rescue puts “meth3 will not run yet, so this will print”

puts “now let us put the creation of meth3 using get. i know, but this  
is a stup  
id example”  
puts “i’m running…”  
puts “pls enter a class definition in one line only:”  
s = gets  
eval(s)  
puts “i’m running…”  
puts c.meth3 rescue puts “now meth3 will run, so this will not print”  
puts “i’m running…”  
puts “ok”

C:\temp\>ruby test.rb  
i’m running…  
i’m running… inside class  
i’m running…  
no c.meth yet, so this will print  
i’m running… inside class; creating method meth now  
i’m running…  
yeow!  
i’m running…  
method meth2 not yet defined  
let us put the creation on a string…  
i’m running…  
i’m running…  
i’m running… inside class; creating method second meth2 now  
now meth2 will run  
i’m running…  
hehey!  
i’m running…  
meth3 will not run yet, so this will print  
now let us put the creation of meth3 using get. i know, but this is a  
stupid exa  
mple  
i’m running…  
pls enter a class definition in one line only:  
class C; def meth3; “hello from meth3”; end;end  
i’m running…  
hello from meth3  
i’m running…  
ok

C:\temp\>

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/5 "2007-08-02T14:25:57Z")

</div>

Hi –

On Thu, 2 Aug 2007, Wayne E. Seguin wrote:

> I do not follow what you mean by “flatten the variable scope of a method  
> definition”.  
> Could you please explain what this means a bit?

Sure. Let’s say you want to do:

a = 1  
class C  
def show\_a  
a # won’t work  
end  
end

The definition of a won’t make it into the scope of the method  
definition #show\_a, because both the class keyword and the def keyword  
start a new local scope.

However, class\_eval and define\_method are both permeable, as to scope.  
So you can do:

a = 1  
class C; end  
C.class\_eval { define\_method(:show\_a) { a } }

C.new.show\_a # 1

The a inside the block given to define\_method is the same a that I  
defined in the first line. That’s what I mean by flattening the  
variable scope.

David

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/6 "2007-08-02T14:27:59Z")

</div>

Kenneth McDonald wrote:

> Having looked through the Class and Method rdoc, and the Reflection section of Pickaxe, and still not seeing anything (even though it was probably right in front of my nose), I’m forced to fall on the mercy of strangers and ask…how does one add a method to a class while the program is running?

I think there are at least two ways to do it, both of which I’ve seen  
in the Ruby Cookbook.

One uses define\_method, as one of the other posters said; the other  
uses module\_eval, IIRC. There’s also an example using module\_eval in  
the Pickaxe - 2nd edition.

Vasudev Ram

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

> **[jugad's Journal](https://jugad.livejournal.com/)**
>
> Vasudev Ram's blog on software innovation

> **[Conversion of other file formats to PDF](https://sourceforge.net/projects/xtopdf/)**
>
> Download Conversion of other file formats to PDF for free. xtopdf: Tools to convert other formats (x) to PDF; x as in math.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 3, 2007, 7:04am UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/7 "2007-08-03T07:04:02Z")

</div>

From: Wayne E. Seguin [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :

# That makes complete sense.

careful. me thinks define\_method is verry different from normal ruby  
methods,  
a. it flattens scope. variables just creep inside  
b. it does not check for arguments

ergo, i’d prefer define\_method be renamed to define\_method!

i suggest define\_method should be documented clearly. it could hang  
nubies, like me 🙂

module\_eval-ing may be closer to ruby methods.

eg,

C:\family\ruby\>cat test.rb  
a = 1  
class C; end  
C.class\_eval {define\_method(:show\_a) {a}}

# this will not err; define\_method methods do not check arguments!

puts C.new.show\_a(1)

# just to show difference…

class C  
def show\_a2  
a  
end  
end

# this will err as usual; i just put a rescue to let prg continue

puts C.new.show\_a2(1) rescue puts “wrong number of args !!”

# I think module\_eval is a closer match to normal methods

s = %q{def show\_a(); a ; end}  
C.module\_eval(s)

# vars will not creep

puts C.new.show\_a() rescue puts “undefined a !!”

# and args will be checked

puts C.new.show\_a(1) rescue puts “wrong number of args !!”

C:\family\ruby\>ruby test.rb  
1  
wrong number of args !!  
undefined a !!  
wrong number of args !!

C:\family\ruby\>

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 2, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/defining-methods-dynamically/111040/8 "2007-08-02T14:35:59Z")

</div>

On Aug 02, 2007, at 08:24 , removed\_email\_address@domain.invalid wrote:

> definition #show\_a, because both the class keyword and the def keyword
> 
> The a inside the block given to define\_method is the same a that I  
> defined in the first line. That’s what I mean by flattening the  
> variable scope.
> 
> David

That makes complete sense. Thank you kindly for the explanation.
