posted: July 5th, 2009 · by: Sven
Ripper is a Ruby parser that ships with Ruby 1.9. Ripper2Ruby is a library that generates a representation of Ruby code which you can modify and/or compile back to Ruby.
- 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.
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.