From: Dave Thomas Date: 2001-07-15T14:32:56+09:00 Subject: [ruby-talk:17865] Re: Creating methods on the fly "HarryO" writes: > That works OK if I do it by having a method in the class that then calls > the global function to define the class's method. However, that means I > have to make sure I call that method for each object of that class. If I understand what you're after, you want to define an instance method in a named class. def add_debug_to(class_name) class_name.class_eval %{ def debug puts "In \#{self} of type \#{self.class}" end } end class Dave end class Bert end add_debug_to(Dave) d = Dave.new d.debug #=> In # of type Dave add_debug_to(eval("Bert")) b = Bert.new b.debug #=> In # of type Bert If the method is fixed, then you can also use mixins to do the same kind of thing module Debugger def debug puts "In #{self} of type #{self.class}" end end class Dave end Dave.class_eval { include(Debugger) } d = Dave.new d.debug Dave