# Parsing Ruby with Ruby

**URL:** <https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294>\
**Category:** Ruby\
**Created:** [February 2, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294 "2006-02-02T14:17:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 2, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/1 "2006-02-02T14:17:29Z")

</div>

Guys,

I’d like to write a code analysis tool that could read Ruby or Rails  
projects (so both standard .rb files and also erb files) and do some  
simple code analysis and metrics, like what methods are called (and how  
many times), etc, etc.

What libraries or tools are out there that might help one accomplish  
this?

Thanks!

John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 2, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/2 "2006-02-02T14:50:45Z")

</div>

John W. wrote:

> John

[http://gems.rubyforge.org/gems/coverage-0.2.gem](http://gems.rubyforge.org/gems/coverage-0.2.gem)

[http://rubyforge.org/projects/zentest/](http://rubyforge.org/projects/zentest/)

[http://alexpooley.com/articles/2006/01/01/ruby-on-rails-test-coverage](http://alexpooley.com/articles/2006/01/01/ruby-on-rails-test-coverage)  
[http://eigenclass.org/hiki.rb?rcov+0.1.0+prerelease](http://eigenclass.org/hiki.rb?rcov+0.1.0+prerelease)  
[http://asplake.blogspot.com/2006/01/test-coverage-with-rcov-and-rake-962.html](http://asplake.blogspot.com/2006/01/test-coverage-with-rcov-and-rake-962.html)

[http://blog.zenspider.com/archives/ruby/parsetree/index.html](http://blog.zenspider.com/archives/ruby/parsetree/index.html)

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 2, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/3 "2006-02-02T15:24:00Z")

</div>

John W. wrote:

> John

Actually, I think you’re looking for libs that expose language  
services, so:

(best place to look)  
[http://rubyforge.org/softwaremap/trove\_list.php?form\_cat=45&discrim=306](http://rubyforge.org/softwaremap/trove_list.php?form_cat=45&discrim=306)

also:  
[http://rubyforge.org/projects/input/](http://rubyforge.org/projects/input/)

[http://rubyforge.org/projects/rubygrammar/](http://rubyforge.org/projects/rubygrammar/)

[http://www.zenspider.com/ZSS/Products/CocoR/index.html](http://www.zenspider.com/ZSS/Products/CocoR/index.html)  
[http://rubyforge.org/projects/coco-ruby/](http://rubyforge.org/projects/coco-ruby/)

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 2, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/4 "2006-02-02T16:15:25Z")

</div>

Gene,

Thanks for the links. Perhaps I should describe what I’m trying to do a  
bit better.

I’d like to write a program that could descend through a directory of  
Ruby  
programs and generate summaries of what methods are called where. So,  
optimal output for what I’d like to do is the file name parsed, the  
methods found that are called within the file, and the classes those  
methods are derived from.

So, I could point this program at a directory containing a Rails program  
and find out, for example, how many times ActionController::Base#render  
was called, and from what files.

Perhaps it’s a bit more complicated that I’d hoped.

Thanks!  
John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 3, 2006, 8:15am UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/5 "2006-02-03T08:15:52Z")

</div>

Do you mean the number of times a method is references or do you mean  
the number of times called while running? The former requires an  
additional script while the latter requires using the ruby profiler  
that ships with the standard distribution (just add the ‘-rprofile’  
option when calling ruby. It will display metrics when your application  
ends).

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 4, 2006, 2:41am UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/6 "2006-02-04T02:41:05Z")

</div>

Timothy G. wrote:

> Do you mean the number of times a method is references or do you mean  
> the number of times called while running? The former requires an  
> additional script while the latter requires using the ruby profiler  
> that ships with the standard distribution (just add the ‘-rprofile’  
> option when calling ruby. It will display metrics when your application  
> ends).

The number of time a method is referenced in the code, not called while  
running. My goal was to take every Rails OSS project I know of that has  
a source repository, run the parser to determine method call counts,  
sort and rank.

Why? The one thing I’m missing in Rails is idiomatic ways of doing  
things, even after reading the PragProg book. The framework is big, and  
some things just haven’t “stuck” yet. Thought this would be an  
interesting perspective of the projects currently out there.

Thanks!  
John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [February 3, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/parsing-ruby-with-ruby/100294/7 "2006-02-03T22:54:20Z")

</div>

On Feb 2, 2006, at 5:14 AM, John W. wrote:

> I’d like to write a code analysis tool that could read Ruby or Rails  
> projects (so both standard .rb files and also erb files) and do some  
> simple code analysis and metrics, like what methods are called (and  
> how  
> many times), etc, etc.
> 
> What libraries or tools are out there that might help one  
> accomplish this?

ParseTree is probably the best tool for this job, it returns the AST  
for ruby methods as a tree of arrays and includes a class for  
processing and rewriting the AST called SexpProcessor. It comes with  
an ABC metrics tool (assignments, branches, calls) and is the  
backbone of the ruby2c project.

[http://rubyforge.org/projects/parsetree/](http://rubyforge.org/projects/parsetree/)

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
