From: Mauricio Fernandez Date: 2006-09-08T00:50:45+09:00 Subject: Re: Has a method been called? On Fri, Sep 08, 2006 at 12:23:10AM +0900, Jan Svitok wrote: > On 9/7/06, aidy 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 ;-) You can do it with rcov's API: RUBY_VERSION # => "1.8.5" require 'rcov' class Foo; def foo; 1 end def bar; 1 end end analyzer = Rcov::CallSiteAnalyzer.new analyzer.run_hooked do f = Foo.new f.foo end def analyzer.executed?(selector); !!defsite(selector) end analyzer.executed? "Foo#foo" # => true analyzer.executed? "Foo#bar" # => false 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). -- Mauricio Fernandez - http://eigenclass.org - singular Ruby