Class: Melbourne::AST::FormalArguments

Inherits:
Node
  • Object
show all
Defined in:
lib/melbourne/ast/definitions.rb

Overview

The formal arguments of a method definition as in:

def method(x) # x is a formal argument of the method
end

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph

Constructor Details

#initialize(line, args, defaults, splat) ⇒ FormalArguments



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/melbourne/ast/definitions.rb', line 178

def initialize(line, args, defaults, splat)
  @line = line
  @defaults = nil
  @block_arg = nil

  if defaults
    defaults = DefaultArguments.new line, defaults
    @defaults = defaults
    @optional = defaults.names

    stop = defaults.names.first
    last = args.each_with_index { |a, i| break i if a == stop }
    @required = args[0, last]
  else
    @required = args.dup
    @optional = []
  end

  if splat.kind_of? Symbol
    args << splat
  elsif splat
    splat = :@unnamed_splat
    args << splat
  end
  @names = args
  @splat = splat
end

Instance Attribute Details

#block_argObject

The block argument if there is one



176
177
178
# File 'lib/melbourne/ast/definitions.rb', line 176

def block_arg
  @block_arg
end

#defaultsObject

The arguments of the method that have default values



168
169
170
# File 'lib/melbourne/ast/definitions.rb', line 168

def defaults
  @defaults
end

#namesObject

The names of the arguments



156
157
158
# File 'lib/melbourne/ast/definitions.rb', line 156

def names
  @names
end

#optionalObject

The names of the optional arguments



164
165
166
# File 'lib/melbourne/ast/definitions.rb', line 164

def optional
  @optional
end

#requiredObject

The names of the required arguments



160
161
162
# File 'lib/melbourne/ast/definitions.rb', line 160

def required
  @required
end

#splatObject

The splat (+*some+) arguments of the method



172
173
174
# File 'lib/melbourne/ast/definitions.rb', line 172

def splat
  @splat
end

Instance Method Details

#arityObject

Gets the arity of the method. The arity is the number of required arguments the method defines.

Example

def method(a, b = 1) # arity is 1 as the second argument is optional
end


218
219
220
# File 'lib/melbourne/ast/definitions.rb', line 218

def arity
  @required.size
end

#required_argsObject

Gets the number of required arguments the method defines.



224
225
226
# File 'lib/melbourne/ast/definitions.rb', line 224

def required_args
  @required.size
end

#splat_indexObject

TODO: document!



235
236
237
238
239
240
241
242
# File 'lib/melbourne/ast/definitions.rb', line 235

def splat_index
  if @splat
    index = @names.size
    index -= 1 if @block_arg
    index -= 1 if @splat.kind_of? Symbol
    index
  end
end

#total_argsObject

Gets the total number of all arguments the method defines.



230
231
232
# File 'lib/melbourne/ast/definitions.rb', line 230

def total_args
  @required.size + @optional.size
end