# Pros and cons on dynamic method generation

**URL:** <https://www.ruby-forum.com/t/pros-and-cons-on-dynamic-method-generation/123232>\
**Category:** Ruby\
**Created:** [December 15, 2007, 7:09am UTC](https://www.ruby-forum.com/t/pros-and-cons-on-dynamic-method-generation/123232 "2007-12-15T07:09:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Judson\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@Judson\_L](https://www.ruby-forum.com/u/Judson_L)\
**Post date:** [December 15, 2007, 7:09am UTC](https://www.ruby-forum.com/t/pros-and-cons-on-dynamic-method-generation/123232/1 "2007-12-15T07:09:28Z")

</div>

So, I know two ways to do something like the following:

class Contrived  
@@examples = {}

def self.create\_example\_accessor(name)  
class\_eval \<\<-EOM  
def #{name}()  
return @@examples[#{name}]  
end  
EOM  
end

def self.relink\_example\_accessor(name)  
alias\_method(:name, :\_example\_accessor)  
end

def \_example\_accessor  
me = /:in `([^’]\*)/.match(caller(0).first)  
return @@examples[me]  
end  
end

I sort of like relink\_example\_accessor for giving better backtraces and  
just  
generally not being evaluating a string. I’d like to have something  
cleaner  
than either, just in general, and I’d like to also have a better  
understanding of the pros and cons on each.

Judson
