1. RubyVM::
  2. AbstractSyntaxTree::
  3. Node

class RubyVM::AbstractSyntaxTree::Node

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

This class is MRI specific.

Public Instance Methods

Source
# File ast.rb, line 205defall_tokensPrimitive.ast_node_all_tokensend

Returns all tokens for the input script regardless the receiver node. Returnsnil ifkeep_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]], ...]
Source
# File ast.rb, line 216defchildrenPrimitive.ast_node_childrenend

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 ornil.

Source
# File ast.rb, line 148deffirst_columnPrimitive.ast_node_first_columnend

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

Source
# File ast.rb, line 140deffirst_linenoPrimitive.ast_node_first_linenoend

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

Source
# File ast.rb, line 224definspectPrimitive.ast_node_inspectend

Returns debugging information about this node as a string.

Source
# File ast.rb, line 164deflast_columnPrimitive.ast_node_last_columnend

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

Source
# File ast.rb, line 156deflast_linenoPrimitive.ast_node_last_linenoend

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

Source
# File ast.rb, line 280deflocationsPrimitive.ast_node_locationsend

Returns location objects associated with the AST node. The returned array containsRubyVM::AbstractSyntaxTree::Location.

Source
# File ast.rb, line 235defnode_idPrimitive.ast_node_node_idend

Returns an internalnode_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.

Source
# File ast.rb, line 247defscript_linesPrimitive.ast_node_script_linesend

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.

Source
# File ast.rb, line 263defsourcelines =script_linesiflineslines =lines[first_lineno-1..last_lineno-1]lines[-1] =lines[-1].byteslice(0...last_column)lines[0] =lines[0].byteslice(first_column..-1)lines.joinelsenilendend

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.

Source
# File ast.rb, line 184deftokensreturnnilunlessall_tokensall_tokens.each_with_object([])do|token,a|loc =token.lastif ([first_lineno,first_column]<=> [loc[0],loc[1]])<=0&&       ([last_lineno,last_column]<=> [loc[2],loc[3]])>=0a<<tokenendendend

Returns tokens corresponding to the location of the node. Returnsnil ifkeep_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:

Source
# File ast.rb, line 132deftypePrimitive.ast_node_typeend

Returns the type of this node as a symbol.

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