Class Tuple
In: lib/more/facets/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

  t1.to_s  #=> "1.2.3"
  t2.to_s  #=> "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   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

Constants

SEGMENT_SEPARATOR = '.'

Attributes

default  [RW] 

Public Class methods

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.

Parses a constraint returning the operation as a lambda.

Public Instance methods

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

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

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

Unique hash value.

These are useful for using a Tuple as a version.

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

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

shift()

Alias for pull

unshift( obj )

Alias for pot

Protected Instance methods

def divider( set=nil )

  return @divider unless set
  @divider = set
  self

end

[Validate]