/
arity.rb
34 lines (32 loc) · 839 Bytes
/
arity.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Laser
module Analysis
# The arity of a method is an instance of Arity. It's basically a range
# with some helper methods.
class Arity < Range
# arguments: [Binding::Base]
def self.for_arglist(arguments)
min, max = 0, 0
arguments.each do |arg|
case arg.kind
when :positional
min += 1
max += 1
when :optional
max += 1
when :rest
max = Float::INFINITY
end
end
min..max
end
def initialize(range)
super(range.begin, range.end, range.exclude_end?)
end
def compatible?(other)
self.first <= other.last && other.first <= self.last
end
EMPTY = Arity.new(0..0)
ANY = Arity.new(0..Float::INFINITY)
end
end
end