Is there a simple way to find a method definition?

Hi,
This example is specific to rspec and rails, but the question is
generic to ruby.
How do I find a method definition short of grep “def
some_method_name”?

Here is an example. I did a simple script/generate rspec_scaffold page
title:string body:text
and got a bunch of pre-generated specs. Very nice, now I am going
through this to try and understand the code. I need to find mock_model
so I can look at it (just an example)

def mock_page(stubs={})
@mock_page ||= mock_model(Page, stubs)
end

Is there a simple tool/procedure to find def mock_model ? (or def
some_method_name)

Thanks in advance

If you’re on a mac and using TextMate, you can use
http://gerd.knops.org/files/CTags.tmbundle.zip - it’s pretty sweet -
once installed you can type a command to index a project and then
another command to search for the definition of any method the cursor
is on.

HTH,
David

I am using Ubuntu, but thanks for the response.
I sure wish textmate was portable. My life would be easier.

You can use ctags on Linux as well, vim definitely supports it.

Otherwise you can use rdoc to generate documentation about your
project and that should list all available methods.

Farrel

Ruby F. [2008-07-23 06:59]:

I need to find mock_model so I can look at it (just an example)

def mock_page(stubs={})
@mock_page ||= mock_model(Page, stubs)
end

Is there a simple tool/procedure to find def mock_model ? (or def
some_method_name)
well, ctags is certainly a viable option. but i recently wrote a
little tool [1] that traces added methods and gives you the method
source right in an interactive irb session. why resort to some
external tool when we have ruby :slight_smile:

add this to your ~/.irbrc:

require ‘rubygems’
require ‘nuggets/util/added_methods/init’

(for production use, you might want to put some conditional around
that; otherwise it would be loaded in every session, which at least
slows things down a bit - not to mention any possible bugs or
side-effects.)

then in script/console:

require ‘spec/rails’ # if not already done
puts AddedMethods[:mock_model]

it’s part of the ruby-nuggets gem, available from rubyforge [2] or
our own gem server [3].

[1]
http://prometheus.rubyforge.org/ruby-nuggets/classes/AddedMethods.html
[2] http://rubyforge.org/projects/prometheus/
[3] http://prometheus.khi.uni-koeln.de/rubygems/

cheers
jens

On Jul 23, 2:40 am, Jens W. [email protected] wrote:

little tool [1] that traces added methods and gives you the method
slows things down a bit - not to mention any possible bugs or
[1]
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: [email protected]://www.prometheus-bildarchiv.de/
That is so helpful, I will try it tonight.
FYI when I went to the [1] url I got a 404 err at:
http://prometheus.rubyforge.org/ruby-nuggets/classes/AddedMethods.html

Thank you in a very large way

Ruby F. [2008-07-23 22:39]:

FYI when I went to the [1] url I got a 404 err at:
http://prometheus.rubyforge.org/ruby-nuggets/classes/AddedMethods.html
oops, sorry, forgot to add the namespace :wink:
http://prometheus.rubyforge.org/ruby-nuggets/classes/Util/AddedMethods.html

Does anyone know of a programmatic way to find the definition? I was
looking
for this the other day.
If a type responds to a method that it inherits or gets through
inclusion of
a module, method_defined? returns true. Is there another method that
only
returns true for the actual “definer”?

I suppose you could iterate through the ancestors recursively and find
the
highest-level ancestor where method_defined? is true.

Thanks,

Dean W.

correction (forgot to add the namespace):

Jens W. [2008-07-23 11:40]:

puts AddedMethods[:mock_model]
make that

puts Util::AddedMethods[:mock_model]

http://prometheus.rubyforge.org/ruby-nuggets/classes/AddedMethods.html
and
http://prometheus.rubyforge.org/ruby-nuggets/classes/Util/AddedMethods.html

cheers
jens

hi dean!

Dean W. [2008-07-27 16:50]:

Does anyone know of a programmatic way to find the definition?
this is exactly what you can do with AddedMethods [1]:

require ‘rubygems’
require ‘nuggets/util/added_methods/init’

require ‘your/library/or/whatever’

matches = Util::AddedMethods.find(
:name => ‘method_name’,
:class => YourClass # optional
)

get the class(es) where matching method(s) were defined

matches.each { |am| puts am[:class] # or am.klass }

assume the first one is the one we’re looking for

am = matches.first

is it a singleton method?

puts am.singleton

where exactly has it been defined?

puts “#{am.file}, line #{am.line}”

now get its source

puts am # implies #to_s, you can also call #extract_source directly

does that help in any way? if you trip over any quirks or even bugs,
or if you have any suggestions, please let me know. as i said
earlier in this thread, you can get ruby-nuggets, which this library
is a part of, from rubyforge [2] or from our own gem server [3].

[1]
http://prometheus.rubyforge.org/ruby-nuggets/classes/Util/AddedMethods.html
[2] http://rubyforge.org/projects/prometheus/
[3] http://prometheus.khi.uni-koeln.de/rubygems/

cheers
jens

Thanks. I’ll give it a try.dean

Ruby F. wrote:

Is there a simple tool/procedure to find def mock_model ? (or def
some_method_name)

There’s a tool to determine some of the parameters:
http://eigenclass.org/hiki/method+arguments+via+introspection

or you could use ruby_parser if you wanted to extract info from the
source :slight_smile:

There’s also a tool to lookup using fastri [i.e. in irb]

[].ri_find
——————————————————– Enumerable#find
enum.detect(ifnone = nil) {| obj | block } => obj or nil
enum.find(ifnone = nil) {| obj | block } => obj or nil
————————————————————————
Passes each entry in enum to block. Returns the first for which
block is not false. If no object matches, calls ifnone and returns
its result when it is specified, or returns nil

(1…10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
(1…100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35

=> nil

http://betterlogic.com/roger/?p=327#comment-1058

Good luck.

But then I have another question–isn’t there a way to to inspect on a
method signature and it says what line the method was defined on?
-=R