rewrite

→ ‘rewrite’

What

Rewrite adds sexp-rewriting metaprogramming to Ruby. Or in plain English, Rewrite is a collection of features for Ruby programming like an implementation of andand. What makes Rewrite’s versions of these features different and possibly useful is that they are implemented by rewriting your Ruby code rather than by opening up classes like Kernel, Object, Nil, or Symbol.

Installing

sudogeminstallrewrite

The basics

In your environment.rb file (Rails) or at the top of your source code file, include Rewrite::With and Rewrite::Prelude:

includeRewrite::WithincludeRewrite::Prelude

This makes the with method available to your souce code, so you can write:

That’s a big win because you can use the metaprogramming features you want without worrying that you are breaking anybody else’s code. If you use an implementation of #andand that relies on opening the Object and Nil classes, how do you know you aren’t (a) breaking some other code somewhere else, or (b) exposing your code to being broken if something else changes the #andand methods?

What problem does rewrite solve?

Recall that when you use the “standard” implementation of things like andand or try, you are openly modifying core classes like Object.

Therefore, you are reaching out and touching every line of code in your project. You probably aren’t breaking everything, but even if the chance of introducing a bug by adopting something like “try” is infinitesimal for each source code file in your project, the chance grows greater and greater as your application grows.

The problem is that you are introducing a change on Object, and everything depends on object. This is very different than introducing a change in your code. In that case, only the other bits of code that directly depend on your code are at risk.

Also, imagine if you introduce try and are careful not to break anything. Now somebody else wakes up one day and decides they need a method that works like Prototype’s Try.these. They call it “try.” They just broke your code, dude! Not only are you making everything dependant upon your version of try, but your code is dependent upon everyone else not breaking try as well. It’s a train-wreck waiting to happen.

Rewrite restricts things like andand or try to your code and your code alone. Sure, if you introduce a bug in your code, you may break things that directly depend on your code. But if you introduce “try” using rewrite instead of modifying Object, you will not reach out across your project and break something entirely unrelated that happens to have defined its own version of try in a completely different way.

How does it work?

Rewrite takes your code, converts it to an sexp with Parse Tree, then rewrites the sexp using one or more rewriters you specify. Finally, it converts the sexp back to Ruby with Ruby2Ruby and evals it. It does this when the code is first read, not every time it is invoked, so we mitigate the “do not use andand in a tight loop” problem.

Note that with the examples, the names “andand” and “try” completely go away. If someone else defines a try method elsewhere, it will not affect your code because your code never executes a method called try.

How to love Rewrite without destroying Ruby

If you write:

includeRewrite::WithincludeRewrite::Prelude...

in Kernel context (such as in Rails’ environment.rb), you are adding methods to the global namespace. You can include Rewrite in one or more classes where you want to use its features:

class PersonincludeRewrite::WithincludeRewrite::Prelude...end

Or you can go 100% safe and avoid adding anything to existing namespaces: