Project

rephrase

0.0
No commit activity in last 3 years
No release in over 3 years
Rephrase: a gem for manipulating Ruby code
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 5.11.3
~> 12.0
>= 0
 Project Readme

Rephrase - a gem for manipulating Ruby code

Gem Version Modulation Test MIT licensed

Summary

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 (???)

How does it do it?

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.

How to use it

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"

How to rephrase code

Further documentation is forthcoming...

Limitations

  • Works only on MRI.
  • Ruby 2.7 or newer.
  • Generated source code will not be formatted identically to the source (indentation, line breaks etc.)