Month: February 2013

Ruby AST introspection

Posted on Updated on

Here Ripper and Sourcify test :

https://gist.github.com/glurp/5058883

and the result:
“`
—————————— ——————————

———-
a=1;b=2;a+b
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
[:program,
[[:assign, [:var_field, [:@ident, “a”, [1, 0]]], [:@int, “1”, [1, 2]]],
[:assign, [:var_field, [:@ident, “b”, [1, 4]]], [:@int, “2”, [1, 6]]],
[:binary,
[:var_ref, [:@ident, “a”, [1, 8]]],
:+,
[:var_ref, [:@ident, “b”, [1, 10]]]]]]
———————————————————————-
x=case a when 1 then 1 ; when ‘ee’ then 2 ; end
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
[:program,
[[:assign,
[:var_field, [:@ident, “x”, [1, 0]]],
[:case,
[:vcall, [:@ident, “a”, [1, 7]]],
[:when,
[[:@int, “1”, [1, 14]]],
[[:@int, “1”, [1, 21]]],
[:when,
[[:string_literal,
[:string_content, [:@tstring_content, “ee”, [1, 31]]]]],
[[:@int, “2”, [1, 40]]],
nil]]]]]]
———————————————————————-
class A < Int; AA=1+2 ; def a() @a*@b+@c end; end
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
[:program,
[[:class,
[:const_ref, [:@const, “A”, [1, 6]]],
[:var_ref, [:@const, “Int”, [1, 10]]],
[:bodystmt,
[[:assign,
[:var_field, [:@const, “AA”, [1, 15]]],
[:binary, [:@int, “1”, [1, 18]], :+, [:@int, “2”, [1, 20]]]],
[:def,
[:@ident, “a”, [1, 28]],
[:paren, [:params, nil, nil, nil, nil, nil]],
[:bodystmt,
[[:binary,
[:binary,
[:var_ref, [:@ivar, “@a”, [1, 32]]],
:*,
[:var_ref, [:@ivar, “@b”, [1, 35]]]],
:+,
[:var_ref, [:@ivar, “@c”, [1, 38]]]]],
nil,
nil,
nil]]],
nil,
nil,
nil]]]]
proc { [1, 2, 3].map { |a| (a ** a) } }
[1, 4, 27]