123456789_123456789_123456789_123456789_123456789_

Class: RubyVM::AbstractSyntaxTree::Node

Relationships & Source Files
Inherits: Object
Defined in: ast.rb,
ast.c

Overview

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

This class is MRI specific.

Instance Method Summary

Instance Method Details

#childrenArray

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.

[ GitHub ]

  
# File 'ast.rb', line 131

def children
  Primitive.ast_node_children
end

#first_columnInteger

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

[ GitHub ]

  
# File 'ast.rb', line 104

def first_column
  Primitive.ast_node_first_column
end

#first_linenoInteger

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

[ GitHub ]

  
# File 'ast.rb', line 96

def first_lineno
  Primitive.ast_node_first_lineno
end

#inspectString

Returns debugging information about this node as a string.

[ GitHub ]

  
# File 'ast.rb', line 139

def inspect
  Primitive.ast_node_inspect
end

#last_columnInteger

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

[ GitHub ]

  
# File 'ast.rb', line 120

def last_column
  Primitive.ast_node_last_column
end

#last_linenoInteger

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

[ GitHub ]

  
# File 'ast.rb', line 112

def last_lineno
  Primitive.ast_node_last_lineno
end

#node_idInteger

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.

[ GitHub ]

  
# File 'ast.rb', line 150

def node_id
  Primitive.ast_node_node_id
end

#script_linesArray

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.

[ GitHub ]

  
# File 'ast.rb', line 162

def script_lines
  Primitive.ast_node_script_lines
end

#sourceString

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.

[ GitHub ]

  
# File 'ast.rb', line 178

def source
  lines = script_lines
  if lines
    lines = lines[first_lineno - 1 .. last_lineno - 1]
    lines[-1] = lines[-1][0...last_column]
    lines[0] = lines[0][first_column..-1]
    lines.join
  else
    nil
  end
end

#typeSymbol

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
[ GitHub ]

  
# File 'ast.rb', line 88

def type
  Primitive.ast_node_type
end