Class: UnboundMethod

Inherits:
Object show all
Defined in:
lib/core/facets/unboundmethod/arguments.rb

Instance Method Summary collapse

Instance Method Details

#argumentsObject

Resolves the arguments of the method to have an identical signiture –useful for preserving arity.

class X
  def foo(a, b); end
  def bar(a, b=1); end
end

foo_method = X.instance_method(:foo)
foo_method.arguments   #=> "a0, a1"

bar_method = X.instance_method(:bar)
bar_method.arguments   #=> "a0, *args"

When defaults are used the arguments must end in “*args”.

CREDIT: Trans


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/core/facets/unboundmethod/arguments.rb', line 21

def arguments
  ar = arity
  case ar <=> 0
  when 1
    args = []
    ar.times do |i|
      args << "a#{i}"
    end
    args = args.join(", ")
  when 0
    args = ""
  else
    ar = -ar - 1
    args = []
    ar.times do |i|
      args << "a#{i}"
    end
    args << "*args"
    args = args.join(", ")
  end
  return args
end