Class: Types::Array

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

Overview

Represents an array type with a specific item type.

“‘ruby type = Types::Array.new(Types::Integer) type.parse([“1”, “2”]) # => [1, 2] “`

Instance Method Summary collapse

Methods included from Generic

#|

Constructor Details

#initialize(item_type) ⇒ Array

Returns a new instance of Array.



20
21
22
# File 'lib/types/array.rb', line 20

def initialize(item_type)
	@item_type = item_type
end

Instance Method Details

#composite?Boolean

Returns:



25
26
27
# File 'lib/types/array.rb', line 25

def composite?
	true
end

#map(values) ⇒ Object

Maps the given values using the item type’s parse method.



32
33
34
# File 'lib/types/array.rb', line 32

def map(values)
	values.map{|value| @item_type.parse(value)}
end

#parse(input) ⇒ Object

Parses the input as an array with the specified item type.



40
41
42
43
44
45
46
47
48
49
# File 'lib/types/array.rb', line 40

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 Array!"
	end
end

#resolveObject

Resolves to the actual Ruby Array class.



63
64
65
# File 'lib/types/array.rb', line 63

def resolve
	::Array
end

#to_rbsObject



57
58
59
# File 'lib/types/array.rb', line 57

def to_rbs
	"Array[#{@item_type.to_rbs}]"
end

#to_sObject



52
53
54
# File 'lib/types/array.rb', line 52

def to_s
	"Array(#{@item_type})"
end