Class | Tuple
In: | lib/facets/more/tuple.rb
Parent: | Object

# Tuple

Tuple is essentially an Array, but Comaparable and Immutable.

A tuple can be made using new or #[] just as one builds an array, or using the to_t method on a string or array. With a string tuple remembers the first non-alphanumeric character as the tuple divider.

## Usage

      t1 = Tuple[1,2,3]
      t2 = Tuple[2,3,4]

      t1 < t2   #=> true
      t1 > t2   #=> false

      t1 = '1.2.3'.to_t
      t2 = '1-2-3'.to_t

      puts t1  #=> 1.2.3
      puts t2  #=> 1-2-3

      t1 == t2  #=> true

Keep in mind that Tuple[1,2,3] is not the same as Tuple[‘1’,’2’,’3’].

### Methods

<< <=> =~ [] [] []= cast_from_array cast_from_string constraint_to_lambda divider each each_index empty? eql? first hash index inspect last length major minor multiton_id new parse_constraint pop pot pull push rindex shift size teeny to_a to_ary to_s to_t to_tuple unshift values

### Included Modules

::Multiton ::Enumerable ::Comparable

### Attributes

default | [RW] |

### Public Class methods

[]( *args )
cast_from_array( arr )
cast_from_string( str, &yld )

Translates a string in the form on a set of numerical and/or alphanumerical characters separated by non-word characters (eg \W+) into a Tuple. The values of the tuple will be converted to integers if they are purely numerical.

      Tuple.cast_from_string('1.2.3a')  #=> [1,2,"3a"]

It you would like to control the interpretation of each value as it is added to the tuple you can supply a block.

      Tuple.cast_from_string('1.2.3a'){ |v| v.upcase }  #=> ["1","2","3A"]

This method is called by String#to_t.

constraint_to_lambda( constraint, &yld )

Parses a constraint returning the operation as a lambda.

multiton_id(arg=0, default=0, &block)
new(arg=0, default=0, &blk)
parse_constraint( constraint, &yld )

### Public Instance methods

<<( obj )

Unlike Array, Tuple#<< cannot act in place becuase Tuple‘s are immutable.

<=>( other )
=~( other )

For pessimistic constraint (like ’~>’ in gems)

[](i)
[]=(i,v)
divider( set=nil )
each( &block )
each_index( &block )
empty?()
eql?( other )

Returns true if two tuple references are for the very same tuple.

first()
hash()

Unique hash value.

index()
inspect()
last()
length()
major()

These are useful for using a Tuple as a version.

minor()
pop()
pot( obj )

Stands for "Put On Top". This method is the opposite of pull and is otherwise known as unshift.

pull()

Pulls a value off the beginning of a tuple. This method is otherwsie known as shift.

push( obj )
rindex()
shift()

Alias for pull

size()
teeny()
to_a()
to_ary()
to_s( divider=nil )
to_t()
to_tuple()
unshift( obj )

Alias for pot

### Protected Instance methods

values()

[Validate]