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

In Cool, Elsewhere, Miscellaneous, Ruby Tricks

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]}]

Vaguely Related Posts (Usually)

5 Comment Responses to “RubyNode – Like ‘.inspect’ but for code instead of data”

  1. #1
    Matt Mower Says:

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

  2. #2
    Peter Cooper Says:

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

  3. #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. #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. #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.