← Home

Ripper2Ruby: modify and recompile your Ruby code

So, the combination Ripper/Ripper2Ruby lets you do similar things as you can do with ParseTree or RubyParser and Ruby2Ruby. The differences are:

  • Ripper requires Ruby 1.9 (I was told it possibly could be compiled to work with Ruby 1.8.x but I don’t know anything further. Please drop me a note if you know how to do this.)
  • Ripper2Ruby builds a full object-oriented representation of Ruby code. That means you can modify the representation much more easily compared to the rough sexp tree that you get from the parsers. It also provides complete information about the node’s original source position, whitespace, comments etc.
  • Therefor with Ripper2Ruby you can recompile the exact copy of the original source code, character by character (that’s not possible with Ruby2Ruby). Ripper2Ruby has been tested with 225 Ruby libraries including Rails, Merb, Ruby Stdlib etc.
  • Ripper2Ruby does more but it’s slower, too.

For example:


src = "I18n.t(:foo)"
code = Ripper::RubyBuilder.build(src)
code.to_ruby # => "I18n.t(:foo)"

foo = code.select(Ruby::Symbol).first
foo.identifier.token = 'bar'
code.to_ruby # => "I18n.t(:bar)"

Ripper2Ruby was build to make it easier to create refactoring tools for Ruby/Rails I18n support (see i18n-tools). Huge thanks go (again) to Torsten Becker, Bestgroup Software & Consulting for making this possible.