W3cubDocs

/Ruby 2.6

class RubyVM::AbstractSyntaxTree::Node

Parent:
Object

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

Public Instance Methods

children → array Show source
static VALUE
rb_ast_node_children(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return node_children(data->ast, data->node);
}

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.

first_column → integer Show source
static VALUE
rb_ast_node_first_column(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return INT2NUM(nd_first_column(data->node));
}

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

first_lineno → integer Show source
static VALUE
rb_ast_node_first_lineno(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return INT2NUM(nd_first_lineno(data->node));
}

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

inspect → string Show source
static VALUE
rb_ast_node_inspect(VALUE self)
{
    VALUE str;
    VALUE cname;
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    cname = rb_class_path(rb_obj_class(self));
    str = rb_str_new2("#<");

    rb_str_append(str, cname);
    rb_str_catf(str, ":%s@%d:%d-%d:%d>",
                node_type_to_str(data->node),
                nd_first_lineno(data->node), nd_first_column(data->node),
                nd_last_lineno(data->node), nd_last_column(data->node));

    return str;
}

Returns debugging information about this node as a string.

last_column → integer Show source
static VALUE
rb_ast_node_last_column(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return INT2NUM(nd_last_column(data->node));
}

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

last_lineno → integer Show source
static VALUE
rb_ast_node_last_lineno(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return INT2NUM(nd_last_lineno(data->node));
}

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

pretty_print(q) Show source
# File lib/pp.rb, line 531
def pretty_print(q)
  q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
    case type
    when :SCOPE
      pretty_print_children(q, %w"tbl args body")
    when :ARGS
      pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
    when :DEFN
      pretty_print_children(q, %w[mid body])
    else
      pretty_print_children(q)
    end
  }
end
pretty_print_children(q, names = []) Show source
# File lib/pp.rb, line 518
def pretty_print_children(q, names = [])
  children.zip(names) do |c, n|
    if n
      q.breakable
      q.text "#{n}:"
    end
    q.group(2) do
      q.breakable
      q.pp c
    end
  end
end
type → symbol Show source
static VALUE
rb_ast_node_type(VALUE self)
{
    struct ASTNodeData *data;
    TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);

    return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
}

Returns the type of this node as a symbol.

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

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.