Steadfast Update II - Rewriting ASTs - Noteflakes
“So, if Prism nodes are immutable, how can we rewrite them? Basically, Prism lets us create a copy of a given node, with specific attributes assigned new values:”
Jump to Highlight ↗
Steadfast is a new Ruby project that aims to provide a new way to dynamically generate Ruby code at runtime by synthesizing and manipulating ASTs, as an alternative to the common technique of calling eval with a dynamically generated string containing Ruby source code.
In the last Steadfast update I discussed the foundations of the project, its general API design, and my work on implementing the basic capacity of turning ASTs back to source code (using Steadfast.to_source), as well as providing some facilities for translating backtrace locations for code derived from AST that were composed of pieces of code from different locations.
Over the last few days, I’ve concentrated on implementing the ability to transform an existing AST by replacing specific nodes therein. Previously, I called this “mutating” an AST, but actually I realized this term might be confusing, since Prism ASTs are inherently immutable, that is, they cannot be modified. Instead, you need to create a copy of an AST node with the requisite changes, so using the verb “mutate” is in fact incorrect. So henceforth I’ll use the term “rewrite” instead, which seems more fitting, since we’re basically taking existing code and rewriting it.
Now, there are some interesting nuances to this, because Prism nodes basically have three different kinds of attributes:
📝 Your Private Notes & Highlights 🔒 Only visible to you
Log in or create a free account to keep private highlights and personal notes on your stored picks.
Featured on Storesso
Are you the creator of noteflakes.com? Share your verified feature badge on your website, or export a custom 4:5 editorial social card for Instagram and X.
Generate a 1080×1350 vertical high-res feature card tailored for social sharing.
Instagram & X Feature Card
Export a polished, high-resolution 4:5 editorial graphic displaying your verified inclusion in the Storesso Discovery Index. Perfect for launch threads, founder updates, and newsletters.