[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Has a method been called?

aidy

9/7/2006 2:56:00 PM

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

4 Answers

Farrel Lifson

9/7/2006 3:22:00 PM

0

On 07/09/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
>
>
>

Have a look at set_trace_func. It's documented in the pickaxe book
online at http://www.rubycentral.com/book/o... (just scroll
down to Tracing Your Program's Execution). You'll probably be
interested in the 'call' events.

Farrel

Jano Svitok

9/7/2006 3:23:00 PM

0

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 ;-)

Patrick Hurley

9/7/2006 3:43:00 PM

0

On 9/7/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> 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 ;-)
>
>

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

Then...

class Foo
def bar
puts :bar
end
flag_use :bar
end

f = Foo.new
p f.bar_called?
f.bar
p f.bar_called?

pth

Mauricio Fernández

9/7/2006 3:51:00 PM

0

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 ;-)

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://eige... - singular Ruby