Skip to content

Instantly share code, notes, and snippets.

@smoll
Last active November 5, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoll/6b73b1df963076bf3767 to your computer and use it in GitHub Desktop.
Save smoll/6b73b1df963076bf3767 to your computer and use it in GitHub Desktop.
Adapted from the unparser usage example, at https://github.com/mbj/unparser#usage
require 'parser/current'
require 'unparser'
require 'byebug'
module YourHelper
def s(type, *children)
Parser::AST::Node.new(type, children)
end
end
include YourHelper
# begin experiments
node = s(:def,
:foo,
s(:args,
s(:arg, :x)
),
s(:send,
s(:lvar, :x),
:+,
s(:int, 3)
)
)
Unparser.unparse(node) # => "def foo(x)\n x + 3\nend"
byebug
puts "eof" # byebug won't pause execution if it's the last line of code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment