Class: Melbourne::AST::IterArguments

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

Overview

Arguments of a code block as in:

m { |arg| some }

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph

Constructor Details

#initialize(line, arguments) ⇒ IterArguments

Returns a new instance of IterArguments.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/melbourne/ast/sends.rb', line 270

def initialize(line, arguments)
  @line = line
  @optional = 0

  @splat_index = -1
  @required_args = 0
  @splat = nil
  @block = nil

  array = []
  case arguments
  when Fixnum
    @arity = 0
    @prelude = nil
  when MAsgn
    arguments.iter_arguments

    if arguments.splat
      @splat = arguments.splat = arguments.splat.value

      @optional = 1
      if arguments.left
        @prelude = :multi
        @arity = -(arguments.left.body.size + 1)
        @required_args = arguments.left.body.size
      else
        @prelude = :splat
        @arity = -1
      end
    elsif arguments.left
      @prelude = :multi
      @arity = arguments.left.body.size
      @required_args = arguments.left.body.size
    else
      @prelude = :multi
      @arity = -1
    end

    @block = arguments.block

    @arguments = arguments
  when nil
    @arity = -1
    @splat_index = -2 # -2 means accept the splat, but don't store it anywhere
    @prelude = nil
  when BlockPass
    @arity = -1
    @splat_index = -2
    @prelude = nil
    @block = arguments
  else # Assignment
    @arguments = arguments
    @arity = 1
    @required_args = 1
    @prelude = :single
  end
end

Instance Attribute Details

#argumentsObject

The actual array of arguments



261
262
263
# File 'lib/melbourne/ast/sends.rb', line 261

def arguments
  @arguments
end

#arityObject

The number of arguments



253
254
255
# File 'lib/melbourne/ast/sends.rb', line 253

def arity
  @arity
end

#optionalObject

The optional arguments



257
258
259
# File 'lib/melbourne/ast/sends.rb', line 257

def optional
  @optional
end

#preludeObject

TODO: document!



249
250
251
# File 'lib/melbourne/ast/sends.rb', line 249

def prelude
  @prelude
end

#required_argsObject Also known as: total_args

The required arguments



268
269
270
# File 'lib/melbourne/ast/sends.rb', line 268

def required_args
  @required_args
end

#splat_indexObject

TODO: document!



264
265
266
# File 'lib/melbourne/ast/sends.rb', line 264

def splat_index
  @splat_index
end

Instance Method Details

#namesObject

TODO: document!



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/melbourne/ast/sends.rb', line 331

def names
  case @arguments
  when MAsgn
    if arguments = @arguments.left.body
      array = arguments.map { |x| x.name }
    else
      array = []
    end

    if @arguments.splat.kind_of? SplatAssignment
      array << @arguments.splat.name
    end

    array
  when nil
    []
  else
    [@arguments.name]
  end
end