| Class | UnboundMethod |
| In: |
lib/core/facets/unboundmethod/arguments.rb
lib/core/facets/unboundmethod/name.rb |
| Parent: | Object |
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
# File lib/core/facets/unboundmethod/arguments.rb, line 21
21: def arguments
22: ar = arity
23: case ar <=> 0
24: when 1
25: args = []
26: ar.times do |i|
27: args << "a#{i}"
28: end
29: args = args.join(", ")
30: when 0
31: args = ""
32: else
33: ar = -ar - 1
34: args = []
35: ar.times do |i|
36: args << "a#{i}"
37: end
38: args << "*args"
39: args = args.join(", ")
40: end
41: return args
42: end
Return the name of the method.
Be aware that in ruby 1.9 UnboundMethod#name is defined already, but it returns a Symbol not a String.
class X
def foo; end
end
meth = X.instance_method(:foo)
meth.name #=> "foo"
CREDIT: Trans
# File lib/core/facets/unboundmethod/name.rb, line 18
18: def name
19: i = to_s.rindex('#')
20: to_s.slice(i+1...-1)
21: end