Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

RubyNode – Like ‘.inspect’ but for code instead of data

By Peter Cooper / June 18, 2006

RubyNode is an interesting Ruby library that spits out semantic representations of code much in the same way the inspect method does with data. Here's a demo:

>> def plus_1(x)
>>   x + 1
>> end
=> nil
>> pp method(:plus_1).body_node.transform
[:scope,
 {:next=>
   [:block,
    [[:args, {:rest=>-1, :cnt=>1, :opt=>false}],
     [:call,
      {:args=>[:array, [[:lit, {:lit=>1}]]],
       :mid=>:+,
       :recv=>[:lvar, {:vid=>:x, :cnt=>2}]}]]],
  :rval=>[:cref, {:next=>false, :clss=>Object}],
  :tbl=>[:x]}]

Comments

  1. Matt Mower says:

    Any idea how this compares with Ryan Davis & Eric Hodel's work with ParseTree?

  2. Peter Cooper says:

    Unfortunately, no, as I haven't looked at that yet. Thanks for the reference though!

  3. Gene says:

    more node info
    http://rubystuff.org/nodewrap/

    http://sean-carley.blogspot.com/2006/04/assignment-in-ruby-simple-scoped.html

    http://www.namikilab.tuat.ac.jp/~sasada/prog/rubynodes/nodes.html

  4. zimbatm says:

    @Matt : With ParseTree, I was not able to parse a whole source file. I can only pass existing classes, modules or methods to it.

    On the other side, RubyNode doesn't seem to have a Sexp processor included like ParseTree.

  5. zenspider says:

    @zimbatm: the latest version of ParseTree will be able to parse whole files. We're releasing that in a couple of days.

    Not having a sexp processor architecture is a biggie, IMO. We consider SexpProcessor to be the strongest feature in ParseTree. Not having a processing framework is probably not quite as bad as not having comprehensive tests.

    @gene: you should point out that Sean's article is on ParseTree, not nodewrap.

Other Posts to Enjoy

Twitter Mentions