class Appointment
def self.listen_to(*methods)
methods.each do |method_sym|
mth = method(method_sym) # <- doesn't find method `something`
define_method(method_sym) do
print "<listen>#{mth.call}</listen>"
end
end
end
def something
print "doing something"
end
listen_to :something
end
Undefined method 'something' for class 'Class'. The problem seems to be that method(:somesymbol) looks in the class’s scope, and not in the instance scope for the method.
How can I access the something-method from within the def self.listen_to-classmethod?
You need to use
instance_method, notmethod:Not part of the question, but the means of wrapping the method will be a bigger issue. I’d use
alias_methodto rename the old method, and usesendto call it.