Class: NRSER::Types::TupleType
- Defined in:
- lib/nrser/types/tuples.rb
Overview
Tuple type - array of fixed length and types (though those could be any).
Constant Summary
Constants inherited from ArrayType
Instance Attribute Summary collapse
-
#types ⇒ Array<NRSER::Types::Type>
readonly
The types of each of the tuple indexes.
Attributes inherited from IsA
Instance Method Summary collapse
- #explain ⇒ String
-
#has_from_s? ⇒ Boolean
‘true` if this type can load values from a string, which is true if all it’s types can load values from strings.
-
#initialize(*types, **options) ⇒ TupleType
constructor
Instantiate a new ‘TupleType`.
-
#items_from_strings(strings) ⇒ Array
Load each value in an array of strings split out by ArrayType#from_s by passing each value to ‘#from_s` in the type of the corresponding index.
-
#test?(value) ⇒ Boolean
Test value for membership.
Methods inherited from ArrayType
Methods inherited from IsA
#==, #custom_from_data, #has_from_data?, #init_from_data?
Methods inherited from Type
#===, #builtin_inspect, #check, #check!, #from_data, #from_s, #has_from_data?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #test, #to_data, #to_s, #union, #xor
Constructor Details
Instance Attribute Details
#types ⇒ Array<NRSER::Types::Type> (readonly)
The types of each of the tuple indexes.
26 27 28 |
# File 'lib/nrser/types/tuples.rb', line 26 def types @types end |
Instance Method Details
#explain ⇒ String
Returns See NRSER::Types::Type#explain.
52 53 54 |
# File 'lib/nrser/types/tuples.rb', line 52 def explain 'Array<(' + @types.map( &:explain ).join( ', ' ) + ')>' end |
#has_from_s? ⇒ Boolean
Returns ‘true` if this type can load values from a string, which is true if all it’s types can load values from strings.
84 85 86 |
# File 'lib/nrser/types/tuples.rb', line 84 def has_from_s? @from_s || @types.all?( &:has_from_s? ) end |
#items_from_strings(strings) ⇒ Array
Load each value in an array of strings split out by ArrayType#from_s by passing each value to ‘#from_s` in the type of the corresponding index.
97 98 99 100 101 |
# File 'lib/nrser/types/tuples.rb', line 97 def items_from_strings strings @types.each_with_index.map { |type, index| type.from_s strings[index] } end |
#test?(value) ⇒ Boolean
Test value for membership.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nrser/types/tuples.rb', line 66 def test? value # Test the super class first return false unless super( value ) # If it's not the right length then it doesn't pass return false unless value.length == @types.length # Test each item type @types.each_with_index.all? { |type, index| type.test value[index] } end |