On Dec 17, 12:42 pm, Chan Sammy <csa... / gmail.com> wrote:
> In the example below, I really can't sort out why using alias_method
> will work, while using alias won't. Could someone shred me some light
> on this? Thanks.
>
> class T
>  def initialize()
>    puts "T's initialize"
>  end
>  def self.redefine
>    alias old_initialize initialize
>    #alias_method :old_initialize, :initialize
>    self.module_eval do
>      define_method :initialize do
>        old_initialize(1)
>      end
>    end
>  end
> end
>
> class S < T
>  def initialize(int)
>    puts "S's initialize:#{int}"
>  end
>  redefine()
> end
> S.new
>
> ==========================
> Using alias:
> in `old_initialize': wrong number of arguments (1 for 0) (ArgumentError)
> (T's initialize was called)
>
> Using alias_method:
> S's initialize:1
> (S's initialize was called, as expected)

Don't know exactly why, but wrapping alias in a class_eval seems to
fix the problem:

class_eval("alias old_initialize initialize")

Using ri, I couldn't even find who exactly alias belongs to (i.e. is
it defined in Kernel, Object, Module, or what, and is it a class
method of one of these?)  Hopefully someone else can weigh in and
clear this up.

Jason