Class: Types::Enumerator
- Inherits:
-
Object
- Object
- Types::Enumerator
- Includes:
- Generic
- Defined in:
- lib/types/enumerator.rb
Overview
Represents an enumerator type with a specific item type.
“‘ruby type = Types::Enumerator(Types::Integer) type.parse([1, 2, 3].each) # => Enumerator for [1, 2, 3] “`
Instance Method Summary collapse
- #composite? ⇒ Boolean
-
#initialize(item_type) ⇒ Enumerator
constructor
A new instance of Enumerator.
-
#map(enumerator) ⇒ Object
Maps the given enumerator using the item type’s parse method.
-
#parse(input) ⇒ Object
Parses the input as an enumerator with the specified item type.
-
#resolve ⇒ Object
Resolves to the actual Ruby Enumerator class.
- #to_rbs ⇒ Object
- #to_s ⇒ Object
Methods included from Generic
Constructor Details
#initialize(item_type) ⇒ Enumerator
Returns a new instance of Enumerator.
20 21 22 |
# File 'lib/types/enumerator.rb', line 20 def initialize(item_type) @item_type = item_type end |
Instance Method Details
#map(enumerator) ⇒ Object
Maps the given enumerator using the item type’s parse method.
32 33 34 |
# File 'lib/types/enumerator.rb', line 32 def map(enumerator) enumerator.map{|value| @item_type.parse(value)} end |
#parse(input) ⇒ Object
Parses the input as an enumerator with the specified item type.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/types/enumerator.rb', line 40 def parse(input) case input when ::Enumerator return input when ::Array return input.each else raise ArgumentError, "Cannot coerce #{input.inspect} into Enumerator!" end end |
#resolve ⇒ Object
Resolves to the actual Ruby Enumerator class.
63 64 65 |
# File 'lib/types/enumerator.rb', line 63 def resolve ::Enumerator end |
#to_rbs ⇒ Object
57 58 59 |
# File 'lib/types/enumerator.rb', line 57 def to_rbs "Enumerator[#{@item_type.to_rbs}]" end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/types/enumerator.rb', line 52 def to_s "Enumerator(#{@item_type})" end |