[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is there a simple way to find a method definition?

Ruby Freak

7/23/2008 5:04:00 AM

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
11 Answers

David Chelimsky

7/23/2008 5:30:00 AM

0

If you're on a mac and using TextMate, you can use
http://gerd.knops.org/files/CTags.tm... - 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

On Tue, Jul 22, 2008 at 11:59 PM, Ruby Freak <twscannell@gmail.com> wrote:
> 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
>
>

Ruby Freak

7/23/2008 5:47:00 AM

0

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

Farrel Lifson

7/23/2008 6:21:00 AM

0

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
--
Aimred - Ruby Development and Consulting
http://www....

Jens Wille

7/23/2008 9:41:00 AM

0

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/AddedMethod...
[2] <http://rubyforge.org/projects/prome...
[3] <http://prometheus.khi.uni-koeln.de/rub...

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: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Ruby Freak

7/23/2008 8:45:00 PM

0

On Jul 23, 2:40 am, Jens Wille <jens.wi...@uni-koeln.de> wrote:
> 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/AddedMethod...
> [2] <http://rubyforge.org/projects/prome...
> [3] <http://prometheus.khi.uni-koeln.de/rub...
>
> 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: jens.wi...@uni-koeln.dehttp://www.prometheus-bild...

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/AddedMe...

Thank you in a very large way

Jens Wille

7/23/2008 8:50:00 PM

0

Ruby Freak [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/AddedMe...
oops, sorry, forgot to add the namespace ;-)
<http://prometheus.rubyforge.org/ruby-nuggets/classes/Util/AddedMethod...

Jens Wille

7/23/2008 8:55:00 PM

0

correction (forgot to add the namespace):

Jens Wille [2008-07-23 11:40]:
> puts AddedMethods[:mock_model]
make that

puts Util::AddedMethods[:mock_model]

> <http://prometheus.rubyforge.org/ruby-nuggets/classes/AddedMethod...
and
<http://prometheus.rubyforge.org/ruby-nuggets/classes/Util/AddedMethod...

cheers
jens

Dean Wampler

7/27/2008 2:50:00 PM

0

[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

Jens Wille

7/27/2008 5:05:00 PM

0

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/AddedMethod...
[2] <http://rubyforge.org/projects/prome...
[3] <http://prometheus.khi.uni-koeln.de/rub...

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: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Dean Wampler

7/27/2008 6:01:00 PM

0

Thanks. I'll give it a try.dean


On Sun, Jul 27, 2008 at 12:04 PM, Jens Wille <jens.wille@uni-koeln.de>wrote=
:

> 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/AddedM...
ml
> >
> [2] <http://rubyforge.org/projects/prome...
> [3] <http://prometheus.khi.uni-koeln.de/rub...
>
> 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: jens.wille@uni-koeln.de
> http://www.prometheus-bild...
>
>


--=20
Dean Wampler
http://www.object...
http://www.aspectprogr...
http://aquarium.rub...
http://www.cont...