J
Jim Cain
Here's another question... I am aliasing and redefining certain methods,
determined at runtime. Normal methods work fine, but methods ending in
'=' are not behaving the same. Here's an example:
module Mymodule
def Mymodule.append_features(klass)
super(klass)
klass.module_eval do
def self.enalias(s)
alias_method(('old_' + s).intern, s.intern)
module_eval <<-EOF
def #{s}(arg)
puts 'hola ' + arg
old_#{s}(arg)
end
EOF
end
end
end
end
class Myclass
def hello(s)
puts 'hello ' + s
end
include Mymodule
end
class MyclassQ
def hello=(s)
puts 'hello ' + s
end
include Mymodule
end
c = Myclass.new
c.hello('1')
Myclass.enalias('hello')
c.hello('2')
c.old_hello('3')
puts '-----'
q = MyclassQ.new
q.hello=('1')
MyclassQ.enalias('hello=')
q.hello=('2')
q.old_hello=('3')
This outputs the following:
hello 1
hola 2
hello 2
hello 3
-----
hello 1
hola 2
hello 3
Notice that, in the class where the method is "hello=" rather than
"hello", the call to the old method fails somehow. Its output is skipped.
Am I missing something obvious here? Are the setters intended to act
differently somehow? BTW I get this output on both 1.6.8 and 1.8.
determined at runtime. Normal methods work fine, but methods ending in
'=' are not behaving the same. Here's an example:
module Mymodule
def Mymodule.append_features(klass)
super(klass)
klass.module_eval do
def self.enalias(s)
alias_method(('old_' + s).intern, s.intern)
module_eval <<-EOF
def #{s}(arg)
puts 'hola ' + arg
old_#{s}(arg)
end
EOF
end
end
end
end
class Myclass
def hello(s)
puts 'hello ' + s
end
include Mymodule
end
class MyclassQ
def hello=(s)
puts 'hello ' + s
end
include Mymodule
end
c = Myclass.new
c.hello('1')
Myclass.enalias('hello')
c.hello('2')
c.old_hello('3')
puts '-----'
q = MyclassQ.new
q.hello=('1')
MyclassQ.enalias('hello=')
q.hello=('2')
q.old_hello=('3')
This outputs the following:
hello 1
hola 2
hello 2
hello 3
-----
hello 1
hola 2
hello 3
Notice that, in the class where the method is "hello=" rather than
"hello", the call to the old method fails somehow. Its output is skipped.
Am I missing something obvious here? Are the setters intended to act
differently somehow? BTW I get this output on both 1.6.8 and 1.8.