Pros and cons on dynamic method generation

J

Judson Lester

[Note: parts of this message were removed to make it a legal post.]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top