# Best way to parse Ruby code?

**URL:** <https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028>\
**Category:** Ruby\
**Created:** [June 10, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028 "2006-06-10T21:15:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 10, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028/1 "2006-06-10T21:15:01Z")

</div>

I’m trying to parse some Ruby code from C#… I just want to scrape out  
the class/module names and the methods defined for each.

Assuming I first throw away comments and string literals, I figured a  
couple of regular expressions to pull out the class and method names  
would do the trick.

But then I realized I need to handle the possibility of nested classes  
(and knowing which methods belong to the inner class, and which ones  
belong the outer class), and my code got real complicated real fast.

Some googling didn’t reveal much to me, but I’m hoping someone will know  
of a well-known algorithm or regular expressions that will help me out?  
Again, I just need to know the classes defined in a file and the methods  
that go with them. No need for 100% perfection, just something that  
will work across 80% of the Ruby files out there.

Any ideas?

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 11, 2006, 4:28am UTC](https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028/2 "2006-06-11T04:28:16Z")

</div>

On Sun, Jun 11, 2006 at 04:15:15AM +0900, Jeff C. wrote:

> I’m trying to parse some Ruby code from C#… I just want to scrape out  
> the class/module names and the methods defined for each.  
> […]  
> Some googling didn’t reveal much to me, but I’m hoping someone will know  
> of a well-known algorithm or regular expressions that will help me out?

```
Well, not an algorithm, but perhaps you can learn from the source of 

```

the  
Ruby programs that do syntax highlighting to Ruby (see the thread  
“Colored  
syntax output” started by Robert MannI)…

```
HTH,
```

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 11, 2006, 5:46am UTC](https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028/3 "2006-06-11T05:46:28Z")

</div>

On Sun, 11 Jun 2006, Esteban Manchado [iso-8859-1] Velázquez wrote:

> HTH,

check out the rdoc source.

-a

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 11, 2006, 7:03am UTC](https://www.ruby-forum.com/t/best-way-to-parse-ruby-code/63028/4 "2006-06-11T07:03:17Z")

</div>

On Jun 10, 2006, at 8:45 PM, removed\_email\_address@domain.invalid wrote:

> > > me out?
> > 
> > Well, not an algorithm, but perhaps you can learn from the  
> > source of the  
> > Ruby programs that do syntax highlighting to Ruby (see the thread  
> > “Colored  
> > syntax output” started by Robert MannI)…
> > 
> > HTH,
> 
> check out the rdoc source.

Which I believe was derived from irb.

There’s also ParseTree, but I believe it is unsuitable for your problem.

Check out the grammarians list:

[http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians](http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians)

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

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