How do I alias_method class methods?

The code below throws the following exception:

/tmp/foo.rb:10:in `alias_method': undefined method `substitute_do_it'
for module `Foo' (NameError)

This confuses me, because module Foo clearly has a substitute_do_it
method. I want the code to print out:

original do_it
substitute do_it

code:

module Foo
  class << self
    def do_it
      puts "original do_it"
    end
    def substitute_do_it
      puts "substitute do_it"
    end
    def do_substitute
      module_eval { alias_method :do_it, :substitute_do_it }
    end
  end
end

Foo.do_it
Foo.do_substitute
Foo.do_it

I thought I understood Ruby… :wink:

        def do_substitute
          module_eval { alias_method :do_it, :substitute_do_it }
        end

         def do_substitute
      class << self
         alias_method :do_it, :substitute_do_it
            end
         end

Guy Decoux

ts decoux@moulon.inra.fr writes:

    def do_substitute
      module_eval { alias_method :do_it, :substitute_do_it }
    end
     def do_substitute
  class << self
     alias_method :do_it, :substitute_do_it
        end
     end

Thanks – I also discovered instance_eval

def do_substitute
  code <<-CODE
  class << self
    alias_method :do_it, :substitute_do_it
  end
  CODE
  instance_eval(code)
end

I went this way because the code is dynamically generated (more
complex than the above example).