Rephrase converts Ruby procs or methods to source code, allowing you to reformat, reinterpret or otherwise manipulate the generated source code. Possible uses include:
- Generating code from DSL blocks.
- Inlining loops.
- Macro exansion.
- World domination (???)
Rephrase uses the RubyVM::AbstractSyntaxTree
API to get the AST of a proc or
method. This allows you to manipulate code at runtime, and to be able to access
its binding.
Use Rephrase.to_source
to get the unmodified source code of a proc or method,
e.g.:
require 'rephrase'
example = proc { 2 + 2 }
Rephrase.to_source(example) #=> "proc do\n2 + 2\nend"
Further documentation is forthcoming...
- Works only on MRI.
- Ruby 2.7 or newer.
- Generated source code will not be formatted identically to the source (indentation, line breaks etc.)