Class: Node::SCOPE

Inherits:
Node show all
Includes:
MethodSig
Defined in:
lib/internal/method/signature/node.rb,
ext/internal/node/nodeinfo.c,
ext/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.4/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.4/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.5/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.5/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.6/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.6/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.7/internal/node/nodeinfo.c,
ext/cached/ruby-1.8.7/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.1/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.1/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.2/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.2/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.3/internal/node/nodeinfo.c,
ext/cached/ruby-1.9.3/internal/node/nodeinfo.c

Overview

Represents a lexical scope.

A new scope is created when a method is invoked. The scope node holds information about local variables and arguments to the method. The first two variables in the local variable table are the implicit variables $_ and $~.

The next variables listed in the local variable table are the arguments to the method. More information about the arguments to the method are stored in the ARGS node, which will either be the first node in the scope or the first node in the BLOCK held by the scope.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodSig

#arguments

Methods inherited from Node

#[], #_dump, _load, #address, #as_code, #as_expression, #as_paren_expression, #bytecode_compile, compile_string, define_code, define_expression, #eval, #flags, #inspect, #members, #nd_file, #nd_line, #nd_type, #obfusc, #pretty_print, #swap, #to_a, #tree, type

Class Method Details

.membersArray

Return an array of strings containing the names of the node class’s members.

Returns:

  • (Array)


2793
2794
2795
2796
# File 'ext/internal/node/nodeinfo.c', line 2793

VALUE node_s_members(VALUE klass)
{
  return rb_iv_get(klass, "__member__");
}

Instance Method Details

#args_nodeObject



28
29
30
31
32
33
34
35
36
# File 'lib/internal/method/signature/node.rb', line 28

def args_node
  if self.next.class == Node::ARGS then
    return self.next
  elsif self.next.head.class == Node::ARGS then
    return self.next.head
  else
    raise "Could not find method arguments"
  end
end

#argument_namesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/internal/method/signature/node.rb', line 10

def argument_names
  local_vars = self.local_vars
  args = self.args_node
  num_required_args = args.cnt
  num_optional_args = 0
  opt = args.opt
  while opt do
    num_optional_args += 1
    opt = opt.next
  end
  num_args = \
    num_required_args + \
    num_optional_args + \
    (rest_arg ? 1 : 0) + \
    (block_arg ? 1 : 0)
  return local_vars[0...num_args]
end

#block_argObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/internal/method/signature/node.rb', line 51

def block_arg
  block = self.next
  if block.class == Node::BLOCK and
     block.next.head.class == Node::BLOCK_ARG then
    # subtract 2 to account for implicit vars
    return block.next.head.cnt - 2
  else
    return nil
  end
end

#local_varsObject



6
7
8
# File 'lib/internal/method/signature/node.rb', line 6

def local_vars
  return self.tbl || []
end

#nextObject

Return the Node’s next member. The return type is either a Node or an Object.



2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
# File 'ext/internal/node/nodeinfo.c', line 2437

static VALUE node_next(VALUE self)
{
  NODE * n;
  Data_Get_Struct(self, NODE, n);

  if(TYPE(n->nd_next) == T_NODE)
  {
    if(1 && nd_type(n) == NODE_OP_ASGN2)
    {
      return wrap_node_as(
        (NODE *)n->nd_next,
        rb_cNodeSubclass[NODE_OP_ASGN2_ARG]);
    }
    else
    {
      return wrap_node((NODE *)n->nd_next);
    }
  }
  else
  {
    return (VALUE)n->nd_next;
  }
}

#rest_argObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/internal/method/signature/node.rb', line 38

def rest_arg
  args_node = args_node()
  rest = args_node.rest()
  if rest.class == Node::LASGN then
    # subtract 2 to account for implicit vars
    return rest.cnt - 2
  elsif not rest
    return nil
  else
    return rest > 0 ? rest - 2 : nil
  end
end

#rvalObject

Return the Node’s rval member. The return type is either a Node or an Object.



2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
# File 'ext/internal/node/nodeinfo.c', line 2586

static VALUE node_rval(VALUE self)
{
  NODE * n;
  Data_Get_Struct(self, NODE, n);

  if(TYPE(n->nd_rval) == T_NODE)
  {
    if(0 && nd_type(n) == NODE_OP_ASGN2)
    {
      return wrap_node_as(
        (NODE *)n->nd_rval,
        rb_cNodeSubclass[NODE_OP_ASGN2_ARG]);
    }
    else
    {
      return wrap_node((NODE *)n->nd_rval);
    }
  }
  else
  {
    return (VALUE)n->nd_rval;
  }
}

#set_optional_args(args, args_node, names) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/internal/method/signature/node.rb', line 62

def set_optional_args(args, args_node, names)
  opt = args_node.opt
  while opt do
    head = opt.head
    if head.class == Node::LASGN then
      args[head.vid] = NodeOptionalArgument.new(
          head.vid, head.value.as_expression, head.value, false, false)
    else
      raise "Unexpected node type: #{opt.class}"
    end
    opt = opt.next
  end
end

#tblObject

Return the Node’s tbl member. The return value is an Array holding names of variables.



2677
2678
2679
2680
2681
2682
# File 'ext/internal/node/nodeinfo.c', line 2677

static VALUE node_tbl(VALUE self)
{
  NODE * n;
  Data_Get_Struct(self, NODE, n);
  return variable_names(n->nd_tbl);
}