RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in RubyVM::AbstractSyntaxTree.

This class is MRI specific.

Instance Methods

Returns all tokens for the input script regardless the receiver node. Returns nil if keep_tokens is not enabled when parse method is called.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
root.all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
root.children[-1].all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]

Returns AST nodes under this one. Each kind of node has different children, depending on what kind of node it is.

The returned array may contain other nodes or nil.

The column number in the source code where this AST’s text began.

The line number in the source code where this AST’s text began.

Returns debugging information about this node as a string.

The column number in the source code where this AST’s text ended.

The line number in the source code where this AST’s text ended.

Returns an internal node_id number. Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

No documentation available
No documentation available

Returns the original source code as an array of lines.

Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

Returns the code fragment that corresponds to this AST.

Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

Also note that this API may return an incomplete code fragment that does not parse; for example, a here document following an expression may be dropped.

Returns tokens corresponding to the location of the node. Returns nil if keep_tokens is not enabled when parse method is called.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
root.tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
root.tokens.map{_1[2]}.join # => "x = 1 + 2"

Token is an array of:

Returns the type of this node as a symbol.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
root.type # => :SCOPE
lasgn = root.children[2]
lasgn.type # => :LASGN
call = lasgn.children[1]
call.type # => :OPCALL