From: "Jesús Gabriel y Galán" Date: 2009-11-20T04:54:05+09:00 Subject: Re: Re-opening an existing module and changing a method On Thu, Nov 19, 2009 at 7:45 PM, Aldric Giacomoni wrote: > Jesús Gabriel y Galán wrote: > >> irb(main):003:1> class << self >> irb(main):004:2> alias :old_sqrt :sqrt >> irb(main):005:2> end > > Ah-ha! My version is underneath. So, what the bit of code I quoted does > is.. It reopens "the class of the module" to make a change in the alias? Yes, module_function creates a method in the singleton class of the module. So you need to enter that singleton class via class << self to be in a place where self is that singleton class, in order to be able to alias that method correctly. > > module Math > >  class << self >    alias :old_sqrt :sqrt >  end > >  def self.sqrt x >    result = old_sqrt(x) >    if result.is_a? Float >      "sqrt(#{x})" >    else >      result >    end >  end > end > > >>> puts Math.sqrt(2) > => sqrt(2) Jesus.