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
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
On Fri, Jul 18, 2003 at 11:19:37PM +0900, Jim Cain wrote:
enalias for hello= will be excuted as bellow:
alias_method(('old_hello=').intern, 'hello='.intern)
module_eval <<-EOF
def hello=(arg)
puts 'hola ' + arg
old_hello=(arg) # just local variable assignment.
end
EOF
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
Wouldn’t self.method_name_here=(arg) do the trick?
Jason Creighton
···
On Fri, 18 Jul 2003 23:19:37 +0900 Jim Cain list@jimcain.us wrote:
enalias for hello= will be excuted as bellow:
alias_method(('old_hello=').intern, 'hello='.intern)
module_eval <<-EOF
def hello=(arg)
puts 'hola ' + arg
old_hello=(arg) # just local variable assignment.
end
EOF
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
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.
enalias for hello= will be excuted as bellow:
alias_method(('old_hello=').intern, 'hello='.intern)
module_eval <<-EOF
def hello=(arg)
puts 'hola ' + arg
old_hello=(arg) # just local variable assignment.
end
EOF
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
Errmm, not obvious to me (a newbie). What’s the significance of the =
suffix in amethod name that makes it behave differently
On Fri, Jul 18, 2003 at 11:52:52PM +0900, Brian Candler wrote:
alias_method(('old_hello=').intern, 'hello='.intern)
module_eval <<-EOF
def hello=(arg)
puts 'hola ' + arg
old_hello=(arg) # just local variable assignment.
end
EOF
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
is always interpreted as an assignment to local variable ‘foo’; and from
that point onwards in that method, ‘foo’ by itself is treated as a local
variable rather than a method. So, a local variable assignment takes
precedence over any method with the same name which might exist. Hence you
cannot call a method ‘foo=’ with the above syntax. You can however specify a
receiver (self.foo=) or use ‘send’, in which case it is unambiguously a
method call.
Ought to be documented here, but doesn’t appear to be
Regards,
Brian.
···
On Sat, Jul 19, 2003 at 12:04:48AM +0900, Gawnsoft wrote:
Of course! Duh. I stared and stared at that one and never saw it from
Ruby’s point of view. Now I just have to figure out how to make it look
like a method call.
Errmm, not obvious to me (a newbie). What’s the significance of the =
suffix in amethod name that makes it behave differently