class NRSER::Types::Tuple

Tuple type - array of fixed length and types (though those could be {.Top}).

@note

Construct {Tuple} types using the {.Tuple} factory.

Attributes

types[R]

The types of each of the tuple indexes.

@return [Array<Type>]

Public Class Methods

new(*types, **options) click to toggle source

Instantiate a new `Tuple`.

@param [Array] types

Tuple value types by their index in the tuples.

Entries are passed through {NRSER::Types.make} to create the type
if needed.

@param [Hash<Symbol, *>] options

Type options; see {Type#initialize}.
Calls superclass method NRSER::Types::ArrayType::new
# File lib/nrser/types/tuples.rb, line 56
def initialize *types, **options
  super **options
  @types = types.map( &NRSER::Types.method(:make) ).freeze
end

Public Instance Methods

default_name() click to toggle source
# File lib/nrser/types/tuples.rb, line 62
def default_name
  'Array<(' + types.map( &:name ).join( ', ' ) + ')>'
end
default_symbolic() click to toggle source
# File lib/nrser/types/tuples.rb, line 67
def default_symbolic
  '[' + types.map( &:symbolic ).join( ', ' ) + ']'
end
explain() click to toggle source

@return [String]

See {Type#explain}
# File lib/nrser/types/tuples.rb, line 75
def explain
  'Array<(' + types.map( &:explain ).join( ', ' ) + ')>'
end
has_from_s?() click to toggle source

@return [Boolean]

`true` if this type can load values from a string, which is true if
*all* it's types can load values from strings.
# File lib/nrser/types/tuples.rb, line 107
def has_from_s?
  @from_s || types.all?( &:has_from_s? )
end
items_from_strings(strings) click to toggle source

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.

@param [Array<String>] strings

@return [Array]

# File lib/nrser/types/tuples.rb, line 120
def items_from_strings strings
  types.each_with_index.map { |type, index|
    type.from_s strings[index]
  }
end
test?(value) click to toggle source

Test value for membership.

@param (see Type#test?) @return (see Type#test?) @raise (see Type#test?)

Calls superclass method
# File lib/nrser/types/tuples.rb, line 89
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