Ripper2Ruby: modify and recompile your Ruby code

posted: July 5th, 2009 · by: Sven

in: Programming · tagged as: , ·  0 comments »

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.

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.

Leave a comment

Name required
E-Mail and Website optional

If you can read this, you don't use a typical webbrowser that plays nice with CSS.
Please do not fill in anything here!

Hint: Markdown will be applied to your comment. If you post any code, be sure to escape underscores (like so: \_) if you do not want them to be converted to an <em>phasis.

artweb design
Sven Fuchs
Grünberger Str. 65
10245 Berlin, Germany


http://www.artweb-design.de

Fon +49 (30) 47 98 69 96
Fax +49 (30) 47 98 69 97