Pros and cons on dynamic method generation

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs