Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

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
    [[:args, {:rest=>-1, :cnt=>1, :opt=>false}],
      {:args=>[:array, [[:lit, {:lit=>1}]]],
       :recv=>[:lvar, {:vid=>:x, :cnt=>2}]}]]],
  :rval=>[:cref, {:next=>false, :clss=>Object}],


  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

  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