A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.ruby-lang.org/en/3.4/RubyVM/AbstractSyntaxTree/Node.html below:

class RubyVM::AbstractSyntaxTree::Node - Documentation for Ruby 3.4

class RubyVM::AbstractSyntaxTree::Node

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

This class is MRI specific.

Public Instance Methods

Source

def all_tokens
  Primitive.ast_node_all_tokens
end

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 
root.children[-1].all_tokens 

Source

def children
  Primitive.ast_node_children
end

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.

Source

def first_column
  Primitive.ast_node_first_column
end

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

Source

def first_lineno
  Primitive.ast_node_first_lineno
end

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

Source

def inspect
  Primitive.ast_node_inspect
end

Returns debugging information about this node as a string.

Source

def last_column
  Primitive.ast_node_last_column
end

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

Source

def last_lineno
  Primitive.ast_node_last_lineno
end

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

Source

def node_id
  Primitive.ast_node_node_id
end

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.

Source

def script_lines
  Primitive.ast_node_script_lines
end

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

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

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

def tokens
  return nil unless all_tokens

  all_tokens.each_with_object([]) do |token, a|
    loc = token.last
    if ([first_lineno, first_column] <=> [loc[0], loc[1]]) <= 0 &&
       ([last_lineno, last_column]   <=> [loc[2], loc[3]]) >= 0
       a << token
    end
  end
end

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 
root.tokens.map{_1[2]}.join 

Token is an array of:

Source

def type
  Primitive.ast_node_type
end

Returns the type of this node as a symbol.

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3