Module: MethodSig

Included in:
Method, UnboundMethod
Defined in:
lib/methodsig.rb

Defined Under Namespace

Classes: Origin, Signature

Instance Method Summary collapse

Instance Method Details

#argument_infoObject

Return a hash mapping each argument name to a description of that argument.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/methodsig.rb', line 126

def argument_info
  names = self.argument_names()
  block_arg = self.block_arg()

  info = {}
  names.each do |name|
    info[name] = name.to_s
  end

  # Optional args
  args_node = args_node()
  set_optional_arg_info(info, args_node, names)

  # Rest arg
  if self.rest_arg then
    rest_name = names[rest_arg]
    info[rest_name] = "*#{rest_name}"
  end

  # Block arg
  if block_arg then
    block_name = names[block_arg]
    info[block_name] = "&#{block_name}"
  end

  return info
end

#argument_namesObject

Return the names of the arguments this method takes, in the order in which they appear in the argument list.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/methodsig.rb', line 7

def argument_names
  if self.body.respond_to?(:tbl) then
    # pre-YARV
    return self.body.tbl || []
  else
    # YARV
    iseq = self.body.body
    local_vars = iseq.local_table
    has_rest_arg = iseq.arg_rest != -1
    has_block_arg = iseq.arg_block != -1
    num_args = \
      iseq.argc + \
      iseq.arg_opt_table.size + \
      (has_rest_arg ? 1 : 0) + \
      (has_block_arg ? 1 : 0)
    return local_vars[0...num_args]
  end
end

#block_argObject

If this method has a “block” argument, that is, it has an argument that is preceded by an ampersand (&) in the argument list, then return its index, otherwise return nil.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/methodsig.rb', line 69

def block_arg
  if self.body.respond_to?(:next) then
    # pre-YARV
    block = self.body.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
  else
    # YARV
    arg_block = self.body.body.arg_block
    return arg_block >= 0 ? arg_block - 1 : nil
  end
end

#originObject

Return a Method::Origin representing where the method was defined.



191
192
193
194
# File 'lib/methodsig.rb', line 191

def origin
  block = body().next
  return Origin.new(block.nd_file, block.nd_line)
end

#rest_argObject

If this method has a “rest” argument, that is, it has an argument that is preceded by an asterisk (*) in the argument list, then return its index, otherwise return nil.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/methodsig.rb', line 46

def rest_arg
  args_node = args_node()
  if args_node then
    # pre-YARV
    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
  else
    # YARV
    rest = self.body.body.arg_rest
    return rest >= 0 ? rest - 1: nil
  end
end