Class: Types::Tuple
Overview
Represents a tuple type with specific item types.
“‘ruby type = Types::Tuple(Types::String, Types::Integer) type.parse([“foo”, “42”]) # => [“foo”, 42] “`
Instance Attribute Summary collapse
-
#item_types ⇒ Object
readonly
Returns the value of attribute item_types.
Instance Method Summary collapse
- #composite? ⇒ Boolean
-
#initialize(item_types) ⇒ Tuple
constructor
A new instance of Tuple.
-
#parse(input) ⇒ Object
Parses the input as a tuple with the specified item types.
-
#resolve ⇒ Object
Resolves to the actual Ruby Array class, since there is no direct support for tuples in Ruby.
- #to_rbs ⇒ Object
- #to_s ⇒ Object
Methods included from Generic
Constructor Details
#initialize(item_types) ⇒ Tuple
Returns a new instance of Tuple.
20 21 22 |
# File 'lib/types/tuple.rb', line 20 def initialize(item_types) @item_types = item_types end |
Instance Attribute Details
#item_types ⇒ Object (readonly)
Returns the value of attribute item_types.
25 26 27 |
# File 'lib/types/tuple.rb', line 25 def item_types @item_types end |
Instance Method Details
#parse(input) ⇒ Object
Parses the input as a tuple with the specified item types.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/types/tuple.rb', line 36 def parse(input) case input when ::String return parse_values(parse_string(input)) when ::Array return parse_values(input) else raise ArgumentError, "Cannot coerce #{input.inspect} into tuple!" end end |
#resolve ⇒ Object
Resolves to the actual Ruby Array class, since there is no direct support for tuples in Ruby.
49 50 51 |
# File 'lib/types/tuple.rb', line 49 def resolve ::Array end |
#to_rbs ⇒ Object
59 60 61 |
# File 'lib/types/tuple.rb', line 59 def to_rbs "[#{@item_types.map(&:to_rbs).join(', ')}]" end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/types/tuple.rb', line 54 def to_s "Tuple(#{@item_types.join(', ')})" end |