Has a method been called?

A

aidy

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
 
F

Farrel Lifson

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/ospace.html (just scroll
down to Tracing Your Program's Execution). You'll probably be
interested in the 'call' events.

Farrel
 
J

Jan Svitok

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

Patrick Hurley

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
 
M

Mauricio Fernandez

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top