【发布时间】:2011-10-22 21:44:14
【问题描述】:
我想跟踪在我构建的类的实例上运行的所有方法。
目前我这样做:
class MyClass
def initialize
@completed = []
end
# Sends a welcome and information pack to the person who requested it
def one_of_many_methods
unless @completed.include? __method__
# Do methody things
@completed.push __method__
end
end
alias :another_name :one_of_many_methods
# Calling myClassInstance.another_name will insert
# :one_of_many_methods into @completed.
# Methody things should not now be done if .another_name
# or .one_of_many_methods is called.
end
但是当我的课堂上有很多方法时,这会变得非常费力。我在重复自己!有没有办法跟踪被调用的方法并只允许它们被调用一次,就像我在上面所做的那样,但不必在每个方法中重复该块?
谢谢!
(PS。我使用的是 Ruby 1.9)
【问题讨论】: