class T::Types::FixedArray
Takes a list of types. Validates each item in an array using the type in the same position in the list.
Attributes
types[R]
Public Class Methods
new(types)
click to toggle source
# File lib/types/types/fixed_array.rb, line 11 def initialize(types) @types = types.map {|type| T::Utils.coerce(type)} end
Public Instance Methods
describe_obj(obj)
click to toggle source
This gives us better errors, e.g.: “Expected [String, Symbol], got [String, String]” instead of “Expected [String, Symbol], got Array”.
@override Base
Calls superclass method
T::Types::Base#describe_obj
# File lib/types/types/fixed_array.rb, line 73 def describe_obj(obj) if obj.is_a?(Array) if obj.length == @types.length item_classes = obj.map(&:class).join(', ') "type [#{item_classes}]" else "array of size #{obj.length}" end else super end end
name()
click to toggle source
@override Base
# File lib/types/types/fixed_array.rb, line 16 def name "[#{@types.join(', ')}]" end
recursively_valid?(obj)
click to toggle source
@override Base
# File lib/types/types/fixed_array.rb, line 21 def recursively_valid?(obj) if obj.is_a?(Array) && obj.length == @types.length i = 0 while i < @types.length if !@types[i].recursively_valid?(obj[i]) return false end i += 1 end true else false end end
valid?(obj)
click to toggle source
@override Base
# File lib/types/types/fixed_array.rb, line 37 def valid?(obj) if obj.is_a?(Array) && obj.length == @types.length i = 0 while i < @types.length if !@types[i].valid?(obj[i]) return false end i += 1 end true else false end end
Private Instance Methods
subtype_of_single?(other)
click to toggle source
@override Base
# File lib/types/types/fixed_array.rb, line 53 def subtype_of_single?(other) case other when FixedArray # Properly speaking, covariance here is unsound since arrays # can be mutated, but sorbet implements covariant tuples for # ease of adoption. @types.size == other.types.size && @types.zip(other.types).all? do |t1, t2| t1.subtype_of?(t2) end else false end end