Class | Tuple |
In: |
lib/more/facets/tuple.rb
|
Parent: | Object |
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.
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’].
SEGMENT_SEPARATOR | = | '.' |
default | [RW] |
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.