ast_manipulation
📜 Extracting and Manipulating AST via RubyVM​
RubyVM::AbstractSyntaxTree allows you to introspect or transform code at the AST level. You can parse a method, alter its node tree, and recompile it—enabling powerful DSLs or ahead‑of‑time optimizations.
require 'rubyvm'
node = RubyVM::AbstractSyntaxTree.parse(<<~RUBY)
def greet(name)
puts "Hello, #{name}!"
end
RUBY
# Walk the AST to replace string interpolation with concatenation
transform! = lambda do |n|
if n.type == :DSTR
parts = n.children.map do |c|
c.type == :STR ? c.children[0].inspect : c.children[0].children[1]
end
new_code = parts.join(' + ')
return RubyVM::AbstractSyntaxTree.parse(new_code)
end
n.children.each_with_index { |ch, i| n.children[i] = transform!.call(ch) if ch.is_a?(RubyVM::AbstractSyntaxTree::Node) }
n
end
new_node = transform!.call(node)
puts new_node.inspect
By recompiling new_node
, you can inject or remove constructs before execution, unlocking dynamic code transformations.