Is there a simple way to find a method definition?

R

Ruby Freak

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
 
R

Ruby Freak

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

Farrel Lifson

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
 
J

Jens Wille

Ruby Freak [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 :)

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

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
R

Ruby Freak

Ruby Freak [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 :)

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

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: (e-mail address removed)://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
 
D

Dean Wampler

[Note: parts of this message were removed to make it a legal post.]

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 Wampler
 
J

Jens Wille

hi dean!

Dean Wampler [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

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
D

Dean Wampler

Thanks. I'll give it a try.dean


hi dean!

Dean Wampler [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 =3D Util::AddedMethods.find(
:name =3D> 'method_name',
:class =3D> 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 =3D 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.ht= ml[2] <http://rubyforge.org/projects/prometheus/>
[3] <http://prometheus.khi.uni-koeln.de/rubygems/>

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv f=FCr Forschung & Lehre
Kunsthistorisches Institut der Universit=E4t zu K=F6ln
Albertus-Magnus-Platz, D-50923 K=F6ln
Tel.: +49 (0)221 470-6668, E-Mail: (e-mail address removed)
http://www.prometheus-bildarchiv.de/


--=20
Dean Wampler
http://www.objectmentor.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
 
R

Roger Pack

Ruby said:
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 :)

There's also a tool to lookup using fastri [i.e. in irb]
——————————————————– 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
 

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
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top