class Ameba::Source::Rewriter


This class performs the heavy lifting in the source rewriting process. It schedules code updates to be performed in the correct order.

For simple cases, the resulting source will be obvious.

Examples for more complex cases follow. Assume these examples are acting on the source puts(:hello, :world). The methods #wrap, #remove, etc. receive a range as the first two arguments; for clarity, examples below use English sentences and a string of raw code instead.

Overlapping deletions:

The overlapping ranges are merged and :hello, :world will be removed.

Multiple actions at the same end points:

Results will always be independent of the order they were given. Exception: rewriting actions done on exactly the same range (covered next).


The resulting string will be puts({:hello => [:everybody]}) and this result is independent of the order the instructions were given in.

Multiple wraps on same range:

The wraps are combined in order given and results would be puts([(:hello)], :world).

Multiple replacements on same range:

The replacements are made in the order given, so the latter replacement supersedes the former and :hello will be replaced by :hey.

Swallowed insertions:

A containing replacement will swallow the contained rewriting actions and :hello, :world will be replaced by :hi.


The updates are organized in a tree, according to the ranges they act on (where children are strictly contained by their parent).

Defined in:



Instance Method Summary

Constructor Detail

def : String) #

[View source]

Instance Method Detail

def code : String #

[View source]
def empty? #

Returns true if no (non trivial) update has been recorded

[View source]
def insert_after(begin_pos, end_pos, content) #

[View source]
def insert_after(pos, content) #

[View source]
def insert_before(begin_pos, end_pos, content) #

[View source]
def insert_before(pos, content) #

[View source]
def process #

Applies all scheduled changes and returns modified source as a new string.

[View source]
def remove(begin_pos, end_pos) #

[View source]
def replace(begin_pos, end_pos, content) #

Replaces the code of the given range with content.

[View source]
def wrap(begin_pos, end_pos, insert_before, insert_after) #

Inserts the given strings before and after the given range.

[View source]