# AST for ruby code

**URL:** <https://www.ruby-forum.com/t/ast-for-ruby-code/139943>\
**Category:** Ruby\
**Created:** [June 9, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943 "2008-06-09T13:09:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 9, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943/1 "2008-06-09T13:09:57Z")

</div>

Hi!  
I want AST for the following code as it is given below. I have install  
ruby\_parser 1.0 on my system. We are able to parse string using  
“RubyParser.new.parse(“1+1 = 2”)” and the result is " s(:str, “1+1=2”)".  
But how I can parse the full source file(given below)?

Further, whenever I try to parse the string containing class or def in  
it, it gives error (it means, it is parsing C code not ruby) I have also  
attached the screenshot for this problem. Please also guide us in this  
mater.

Ruby Code:  
…readfile.rb

class TestClass

```
 def method1(a,b)
    sum = a + b
    puts sum
 end

 def method2(*c)
    l = c.length
    puts l
 end

```

end

Desired AST Tree:  
…

s(:class, :TestClass, s(:const, :Object), s(:defn, :method1, s(:scope,  
s(:block, s(:args, :a, :b), s(:lasgn, :sum, s(:call, s(:lvar, :a), :+,  
s(:array, s(:lvar, :b)))), s(:fcall, :puts, s(:array, s(:lvar,  
:sum)))))), s(:defn, :method2, s(:scope, s(:block, s(:args, :"\*c"),  
s(:lasgn, :l, s(:call, s(:lvar, :c), :length)), s(:fcall, :puts,  
s(:array, s(:lvar, :l)))))))

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 1:06am UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943/2 "2008-06-10T01:06:32Z")

</div>

On Jun 9, 2008, at 04:09 , John M. wrote:

> But how I can parse the full source file(given below)?

RubyParser.new.parse(File.read(path))

> Further, whenever I try to parse the string containing class or def in  
> it, it gives error (it means, it is parsing C code not ruby) I have  
> also

because you’re throwing individual lines at the parser when it expects  
complete source.

RubyParser.new.parse(“class Blah”) is not valid ruby.

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 9, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943/3 "2008-06-09T21:31:01Z")

</div>

On Jun 9, 1:09 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def method2(*c)
> 
> ```
> 
> s(:array, s(:lvar, :b)))), s(:fcall, :puts, s(:array, s(:lvar,  
> :sum)))))), s(:defn, :method2, s(:scope, s(:block, s(:args, :“\*c”),  
> s(:lasgn, :l, s(:call, s(:lvar, :c), :length)), s(:fcall, :puts,  
> s(:array, s(:lvar, :l)))))))
> 
> Attachments:[http://www.ruby-forum.com/attachment/2131/error.GIF](http://www.ruby-forum.com/attachment/2131/error.GIF)

On line 14 of your sample you’re starting a block (well, not  
actually), just replace the |file| just just file and see if it works.

HTH,

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 1:42am UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943/4 "2008-06-10T01:42:15Z")

</div>

You are trying to parse it line by line but it won’t work in that way.  
Just parse whole file:

sexp = RubyParser.new.parse(File.read(“readline.rb”))

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [June 10, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/ast-for-ruby-code/139943/5 "2008-06-10T21:45:06Z")

</div>

RadosÅ‚aw BuÅ‚at wrote:

> You are trying to parse it line by line but it won’t work in that way.  
> Just parse whole file:
> 
> sexp = RubyParser.new.parse(File.read(“readline.rb”))
> 
> –  
> Radosï¿½aw Buï¿½at
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mï¿½j blog

* * *

One more help from you that this code is working perfectly on Ruby IDE,  
can u tell me how does it works on Ruby Rail. Because when i run it  
using ruby on rails, it is not displaying anything and also no errors.  
Even I have properly include rubygems and ruby\_parser.

Even Rails server stops working

\<%  
require “rubygems”  
require “ruby\_parser”

sexpfaulty1 = RubyParser.new.parse(File.read(“faulty\_0.rb”))

%\>

\<%= sexpfaulty1 %\>
