Class: Types::Tuple

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/tuple.rb

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

Instance Method Summary collapse

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_typesObject (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

#composite?Boolean

Returns:



28
29
30
# File 'lib/types/tuple.rb', line 28

def composite?
	true
end

#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

#resolveObject

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_rbsObject



59
60
61
# File 'lib/types/tuple.rb', line 59

def to_rbs
	"[#{@item_types.map(&:to_rbs).join(', ')}]"
end

#to_sObject



54
55
56
# File 'lib/types/tuple.rb', line 54

def to_s
	"Tuple(#{@item_types.join(', ')})"
end