C
Chan Sammy
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 ld_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)
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 ld_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)