"HarryO" <harryo / zipworld.com.au> 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 #<Dave:0x401b3770> of type Dave

  add_debug_to(eval("Bert"))
  b = Bert.new
  b.debug     #=> In #<Bert:0x401b320c> 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