Class: Acclaim::Option::Arity
- Inherits:
-
Object
- Object
- Acclaim::Option::Arity
- Defined in:
- lib/acclaim/option/arity.rb
Overview
Represents the the number of arguments an option can take, both mandatory and optional.
Instance Attribute Summary collapse
-
#minimum ⇒ Object
(also: #required)
The minimum number of arguments.
-
#optional ⇒ Object
The number of optional arguments.
Instance Method Summary collapse
-
#==(arity) ⇒ Object
(also: #===)
Converts both
self
andarity
to an array and compares them. -
#bound? ⇒ true, false
Whether the option must take a finite number of arguments.
-
#eql?(arity) ⇒ true, false
Same as == but does not compare directly with arrays.
-
#hash ⇒ Integer
Equivalent to:.
-
#initialize(minimum = 0, optional = 0) ⇒ Arity
constructor
Initializes an arity with the given number of required and optional parameters.
-
#inspect ⇒ String
Returns a string in the following format:.
-
#only?(n) ⇒ true, false
Whether the option takes
n
and onlyn
parameters. -
#to_a ⇒ Array<Integer>
(also: #to_ary, #to_array)
Converts this arity to an array in the form of:.
-
#to_s ⇒ String
Returns a string in the following format:.
-
#total ⇒ Integer?
The total number of parameters that the option may take, or
nil
if the option can take an infinite number of arguments. -
#unlimited? ⇒ true, false
Whether the option can take an unlimited number of arguments.
-
#zero? ⇒ true, false
(also: #none?)
Whether the option takes no parameters.
Constructor Details
#initialize(minimum = 0, optional = 0) ⇒ Arity
Initializes an arity with the given number of required and optional parameters. If the latter is less than zero, then it means the option may take infinite parameters, as long as it takes the minimum number of parameters.
28 29 30 |
# File 'lib/acclaim/option/arity.rb', line 28 def initialize(minimum = 0, optional = 0) @minimum, @optional = minimum, optional end |
Instance Attribute Details
#minimum ⇒ Object Also known as: required
The minimum number of arguments.
14 15 16 |
# File 'lib/acclaim/option/arity.rb', line 14 def minimum @minimum end |
#optional ⇒ Object
The number of optional arguments.
17 18 19 |
# File 'lib/acclaim/option/arity.rb', line 17 def optional @optional end |
Instance Method Details
#==(arity) ⇒ Object Also known as: ===
Converts both self
and arity
to an array and compares them. This is so that comparing directly with an array is possible:
Arity.new(1, 3) == [1, 3]
=> true
100 101 102 |
# File 'lib/acclaim/option/arity.rb', line 100 def ==(arity) to_a == arity.to_a end |
#bound? ⇒ true, false
Whether the option must take a finite number of arguments.
61 62 63 |
# File 'lib/acclaim/option/arity.rb', line 61 def bound? not unlimited? end |
#eql?(arity) ⇒ true, false
Same as == but does not compare directly with arrays.
110 111 112 |
# File 'lib/acclaim/option/arity.rb', line 110 def eql?(arity) minimum == arity.minimum and optional == arity.optional end |
#hash ⇒ Integer
Equivalent to:
to_a.hash
91 92 93 |
# File 'lib/acclaim/option/arity.rb', line 91 def hash to_a.hash end |
#inspect ⇒ String
Returns a string in the following format:
#<Acclaim::Option::Arity minimum +optional>
132 133 134 |
# File 'lib/acclaim/option/arity.rb', line 132 def inspect "#<#{self.class} #{to_s}>" end |
#only?(n) ⇒ true, false
Whether the option takes n
and only n
parameters.
36 37 38 |
# File 'lib/acclaim/option/arity.rb', line 36 def only?(n) optional.zero? and minimum == n end |
#to_a ⇒ Array<Integer> Also known as: to_ary, to_array
Converts this arity to an array in the form of:
[ required, optional ]
79 80 81 |
# File 'lib/acclaim/option/arity.rb', line 79 def to_a [ minimum, optional ] end |
#to_s ⇒ String
Returns a string in the following format:
minimum +optional
The value of optional will be ∞ if this arity is not bound.
122 123 124 |
# File 'lib/acclaim/option/arity.rb', line 122 def to_s "#{minimum} +#{unlimited? ? '∞' : optional}" end |
#total ⇒ Integer?
The total number of parameters that the option may take, or nil
if the option can take an infinite number of arguments.
69 70 71 |
# File 'lib/acclaim/option/arity.rb', line 69 def total if bound? then minimum + optional else nil end end |
#unlimited? ⇒ true, false
Whether the option can take an unlimited number of arguments.
53 54 55 |
# File 'lib/acclaim/option/arity.rb', line 53 def unlimited? optional < 0 end |
#zero? ⇒ true, false Also known as: none?
Whether the option takes no parameters.
44 45 46 |
# File 'lib/acclaim/option/arity.rb', line 44 def zero? only? 0 end |