Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

RubyVM::InstructionSequence#syntax_tree

iseq.syntax_tree → Ruby::Prism::Node | RubyVM::AbstractSyntaxTree::Node | nil

Returns the AST node that this instruction sequence was compiled from, by re-parsing the source with the same parser that compiled it: a Ruby::Prism::Node if it was compiled by prism, or a RubyVM::AbstractSyntaxTree::Node if it was compiled by parse.y.

Returns nil whenever the node cannot be retrieved reliably. For example: the source is not available (such as eval’ed code without RubyVM.keep_script_lines enabled), or the source file has been modified since it was compiled.

The node is a Ruby::Prism::Node, not a node of the prism gem: Ruby::Prism is the parser built into the interpreter, so the returned tree always corresponds to the running code regardless of which version of the prism gem is installed.

This method is experimental and might change without notice.