Class: Tuple
Overview
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 t1 > t2
t1 = '1.2.3'.to_t
t2 = '1-2-3'.to_t
puts t1 puts t2
t1 == t2
Keep in mind that Tuple is not the same as Tuple.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Comparable
#at_least, #at_most, #cap, #clip, #cmp
Methods included from Enumerable
#**, #accumulate, cart, cartesian_product, #cartesian_product, #cluster_by, #collect_if, #collect_with_index, combinations, #combos, #commonality, #compact_collect, #count, #divide, #each_by, #each_combination, #each_combo, #each_pair, #each_permutation, #eachn, #elementwise, #entropy, #every, #every!, #filter_collect, #frequency, #group_by, #ideal_entropy, #inject!, #injecting, #map_send, #mash, #mode, #modulate, #none?, #nonuniq, #occur, #one?, #permutation, #permutation_number, #probability, #split, #sum, #threaded_map, #threaded_map_send, #to_elem, #to_h, #to_hash, #uniq_by
Methods included from Multiton
#clone, #dup
Constructor Details
#initialize(arg = 0, default = 0, &blk) ⇒ Tuple
Returns a new instance of Tuple.
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/more/facets/tuple.rb', line 80
def initialize(arg=0, default=0, &blk)
if block_given?
@values = []
arg.times { |i| @values << blk[i] }
elseif Integer === arg
@values = [ default ] * arg
else
@values = arg.to_ary
end
@default = default
@divider = '.'
end
|
Instance Attribute Details
Returns the value of attribute default.
93
94
95
|
# File 'lib/more/facets/tuple.rb', line 93
def default
@default
end
|
Class Method Details
.[](*args) ⇒ Object
211
212
213
|
# File 'lib/more/facets/tuple.rb', line 211
def []( *args )
instance( args )
end
|
.cast_from_array(arr) ⇒ Object
242
243
244
|
# File 'lib/more/facets/tuple.rb', line 242
def cast_from_array( arr )
self.instance( arr )
end
|
.cast_from_string(str, &yld) ⇒ Object
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')
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 }
This method is called by String#to_t.
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/more/facets/tuple.rb', line 229
def cast_from_string( str, &yld )
args = str.to_s.split(/\W+/)
div = /\W+/.match( str.to_s )[0]
if block_given?
args = args.collect{ |a| yld[a] }
else
args = args.collect { |i| /^[0-9]+$/ =~ i ? i.to_i : i }
end
self.instance( args ).divider( div )
end
|
.constraint_to_lambda(constraint, &yld) ⇒ Object
Parses a constraint returning the operation as a lambda.
248
249
250
251
|
# File 'lib/more/facets/tuple.rb', line 248
def constraint_to_lambda( constraint, &yld )
op, val = *parse_constraint( constraint, &yld )
lambda { |t| t.send(op, val) }
end
|
.multiton_id(arg = 0, default = 0, &block) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/more/facets/tuple.rb', line 68
def self.multiton_id(arg=0, default=0, &block)
if block_given?
values = []
arg.times { |i| values << block[i] }
elseif Integer === arg
values = [ default ] * arg
else
values = arg.to_ary
end
values
end
|
.parse_constraint(constraint, &yld) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/more/facets/tuple.rb', line 253
def parse_constraint( constraint, &yld )
constraint = constraint.strip
re = %r{^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?[-.]\d+)*)$}
if md = re.match( constraint )
if op = md[1]
op = '=~' if op == '~>'
op = '==' if op == '='
val = cast_from_string( md[2], &yld ) else
op = '=='
val = cast_from_string( constraint, &yld ) end
else
raise ArgumentError, "invalid constraint"
end
return op, val
end
|
Instance Method Details
Unlike Array, Tuple#<< cannot act in place becuase Tuple’s are immutable.
149
150
151
|
# File 'lib/more/facets/tuple.rb', line 149
def <<( obj )
self.class.instance( to_a << obj )
end
|
#<=>(other) ⇒ Object
175
176
177
178
179
180
181
182
|
# File 'lib/more/facets/tuple.rb', line 175
def <=>( other )
other = other.to_t
[size, other.size].max.times do |i|
c = self[i] <=> other[i]
return c if c != 0
end
0
end
|
#=~(other) ⇒ Object
For pessimistic constraint (like ‘~>’ in gems)
185
186
187
188
189
190
|
# File 'lib/more/facets/tuple.rb', line 185
def =~( other )
other = other.to_t
upver = other.dup
upver[0] += 1
self >= other and self < upver
end
|
136
137
138
|
# File 'lib/more/facets/tuple.rb', line 136
def [](i)
@values.fetch(i,@default)
end
|
#[]=(i, v) ⇒ Object
140
141
142
|
# File 'lib/more/facets/tuple.rb', line 140
def []=(i,v)
@values[i] = v
end
|
#divider(set = nil) ⇒ Object
95
96
97
98
99
|
# File 'lib/more/facets/tuple.rb', line 95
def divider( set=nil )
return @divider unless set
@divider = set
self
end
|
#each(&block) ⇒ Object
128
129
130
|
# File 'lib/more/facets/tuple.rb', line 128
def each( &block )
@values.each( &block )
end
|
#each_index(&block) ⇒ Object
132
133
134
|
# File 'lib/more/facets/tuple.rb', line 132
def each_index( &block )
@values.each_index( &block )
end
|
#empty? ⇒ Boolean
122
123
124
125
126
|
# File 'lib/more/facets/tuple.rb', line 122
def empty?()
return true if @values.empty?
return true if @values == [ @default ] * @values.size
false
end
|
#eql?(other) ⇒ Boolean
Returns true if two tuple references are for the very same tuple.
170
171
172
173
|
# File 'lib/more/facets/tuple.rb', line 170
def eql?( other )
return true if object_id == other.object_id
end
|
192
|
# File 'lib/more/facets/tuple.rb', line 192
def first() @values.first end
|
201
202
203
204
205
|
# File 'lib/more/facets/tuple.rb', line 201
def hash
to_a.hash
end
|
144
|
# File 'lib/more/facets/tuple.rb', line 144
def index() @values.index end
|
107
|
# File 'lib/more/facets/tuple.rb', line 107
def inspect() to_a.inspect end
|
193
|
# File 'lib/more/facets/tuple.rb', line 193
def last() @values.last end
|
120
|
# File 'lib/more/facets/tuple.rb', line 120
def length() @values.size end
|
These are useful for using a Tuple as a version.
196
|
# File 'lib/more/facets/tuple.rb', line 196
def major() @values.first end
|
197
|
# File 'lib/more/facets/tuple.rb', line 197
def minor() @values.at(1) end
|
153
|
# File 'lib/more/facets/tuple.rb', line 153
def pop() Tuple.instance( to_a.pop ) end
|
#pot(obj) ⇒ Object
Also known as:
unshift
Stands for “Put On Top”. This method is the opposite of #pull and is otherwise known as #unshift.
163
|
# File 'lib/more/facets/tuple.rb', line 163
def pot( obj ) Tuple.instance( to_a.unshift(obj) ) end
|
#pull ⇒ Object
Also known as:
shift
Pulls a value off the beginning of a tuple. This method is otherwsie known as #shift.
159
|
# File 'lib/more/facets/tuple.rb', line 159
def pull() Tuple.instance( to_a.shift ) end
|
#push(obj) ⇒ Object
155
|
# File 'lib/more/facets/tuple.rb', line 155
def push( obj ) Tuple.instance( to_a.push(obj) ) end
|
145
|
# File 'lib/more/facets/tuple.rb', line 145
def rindex() @values.rindex end
|
119
|
# File 'lib/more/facets/tuple.rb', line 119
def size() @values.size end
|
198
|
# File 'lib/more/facets/tuple.rb', line 198
def teeny() @values.at(2) end
|
112
|
# File 'lib/more/facets/tuple.rb', line 112
def to_a() Array(@values) end
|
113
|
# File 'lib/more/facets/tuple.rb', line 113
def to_ary() Array(@values) end
|
#to_s(divider = nil) ⇒ Object
115
116
117
|
# File 'lib/more/facets/tuple.rb', line 115
def to_s( divider=nil )
@values.join(divider||@divider)
end
|
109
|
# File 'lib/more/facets/tuple.rb', line 109
def to_t() self end
|
110
|
# File 'lib/more/facets/tuple.rb', line 110
def to_tuple() self end
|