Last Updated: February 25, 2016
·
807
· sheerun

Get fullblown AST of ruby code without using any gem

This feature is already built in 1.9.3 Std-lib:

require 'ripper'
require 'pp'

Ripper.sexp("def m(a) nil end")

It's going to return:

[:program,
   [[:def,
    [:@ident, "m", [1, 4]],
    [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil]],
    [:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]