Have a look at set_trace_func. It's documented in the pickaxe book
online at http://www.rubycentral.com/book/ospace.html (just scroll
down to Tracing Your Program's Execution). You'll probably be
interested in the 'call' events.
If it's enough for you to know that off-line, you can use rcov or
ruby-prof for that. Run rcov and see whether it's code was visited. If
you need to know in the runtime, that's another story
You can also whip up a little meta programming mess (warning thrown
together, collision causing, not thread safe code follows):
class Module
def flag_use(sym)
self.instance_eval do
define_method "#{sym}_called?" do
false
end
flag_name = "use_flagged_#{sym}".to_sym
alias_method flag_name, sym
define_method sym do
self.class.instance_eval do
alias_method sym, flag_name
define_method "#{sym}_called?" do
true
end
end
flag_name
end
end
end
end
On 9/7/06, aidy <aidy.rutter@gmail.com> wrote:
> Hi,
> Is it possible to find out if a method has been called except by adding
> a flag to it?
>
> class Whatever
> @flag = 0
>
> def method_called
> @flag += 1
> end
>
> def has_method_been_called
> false
> return true if @flag > 0
> end
>
> end
>
> Cheers
>
> Aidy
If it's enough for you to know that off-line, you can use rcov or
ruby-prof for that. Run rcov and see whether it's code was visited. If
you need to know in the runtime, that's another story
However, in this case I'd rather intercept the method call, record it and
redispatch (standard alias_method/module_eval idiom, or define_method if the
redefined methods take no blocks or you're using Ruby 1.9).
···
On Fri, Sep 08, 2006 at 12:23:10AM +0900, Jan Svitok wrote:
On 9/7/06, aidy <aidy.rutter@gmail.com> wrote:
>class Whatever
> @flag = 0
>
> def method_called
> @flag += 1
> end
>
> def has_method_been_called
> false
> return true if @flag > 0
> end
>
>end
If it's enough for you to know that off-line, you can use rcov or
ruby-prof for that. Run rcov and see whether it's code was visited. If
you need to know in the runtime, that's another story