sixarm_ruby_ramp 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,483 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Enumberable extensions
4
+
5
+ module Enumerable
6
+
7
+
8
+ ########################################################################
9
+ #
10
+ # typecast
11
+ #
12
+ ########################################################################
13
+
14
+ # Convert an enumerable to a hash.
15
+ #
16
+ # @return [Hash<Object,Object>] a hash of the enumerable's items
17
+ #
18
+ # @example
19
+ # array=[[:a, :b],[:c, :d],[:e, :f]]
20
+ # array.to_h => {:a=>:b, :c=>:d, :e=>:f}
21
+ #
22
+ # If a key occurs more than once, then this will automatically
23
+ # convert the value to an array of the keys' values.
24
+ #
25
+ # @example
26
+ # array=[[:a,:b],[:a,:c],[:a,:d]]
27
+ # array.to_h => {:a=>[:b, :c, :d]}
28
+
29
+ def to_h
30
+ hash={}
31
+ dupe={}
32
+ each{|key,val|
33
+ if hash.key? key
34
+ if dupe.key? key
35
+ hash[key] << val
36
+ else
37
+ hash[key]=[hash[key]]
38
+ hash[key] << val
39
+ dupe[key]=true
40
+ end
41
+ else
42
+ hash[key]=val
43
+ end
44
+ }
45
+ return hash
46
+ end
47
+
48
+
49
+ # Convert the enumerable to a hash by mapping each item to a pair [index ,item]
50
+ #
51
+ # @return [Hash<Integer,Object>] a hash of the enumerable's items
52
+ #
53
+ # @example
54
+ # strings = ["red","blue","green"]
55
+ # strings.index_by{|a| a.size]}
56
+ # => {3 => "red", 4 => "blue", 5 => "green"}
57
+ #
58
+ # Rails has this method.
59
+ #
60
+ # From http://stackoverflow.com/questions/412771/cleanest-way-to-create-a-hash-from-an-array
61
+ #
62
+ # @see #hash_by
63
+
64
+ def index_by
65
+ inject({}) {|hash, elem| hash.merge!(yield(elem) => elem) }
66
+ end
67
+
68
+
69
+ # Convert the enumerable to a hash by mapping each item to a pair [item, new item]
70
+ #
71
+ # @return [Hash<Object,Object>] a hash of the enumerable's items
72
+ #
73
+ # @example
74
+ # strings = ["red","blue","green"]
75
+ # strings.hash_by{|a| [a.size, a.upcase]}
76
+ # => {3 => "RED", 4 => "BLUE", 5 => "GREEN"}
77
+ #
78
+ # @see #index_by
79
+
80
+ def hash_by
81
+ map{|item| yield(item)}.to_h
82
+ end
83
+
84
+
85
+
86
+ ########################################################################
87
+ #
88
+ # map_to_xxx
89
+ #
90
+ ########################################################################
91
+
92
+
93
+ # Map each item => item.id
94
+ #
95
+ # @return [Enumerable<Object>] an list of each item.id
96
+ #
97
+ # @example
98
+ # users = User.find(:all)
99
+ # users.map_id => [1,2,3,4,...]
100
+ #
101
+ # A typical use is to convert a list of ActiveRecord items to a list of id items.
102
+ #
103
+ # This method is a fast way to get the same results as items.map(&:id)
104
+
105
+ def map_id
106
+ map{|item| item.id}
107
+ end
108
+
109
+
110
+ # Map each item => item.to_a
111
+ #
112
+ # @return [Enumberable<Array<Object>>] a list of each item.to_a
113
+ #
114
+ # @example
115
+ # set1 = Set.new([:a,:b,:c])
116
+ # set2 = Set.new([:d,:e,:f])
117
+ # set3 = Set.new([:g,:h,:i])
118
+ # sets = [set1, set2, set3]
119
+ # sets.map_to_a => [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]]
120
+ #
121
+ # A typical use is to convert a list with Set items to a list of Array items,
122
+ # so you can more easily iterate over the the Array items.
123
+ #
124
+ # See http://www.ruby-doc.org/core/classes/Enumerable.html#M003148
125
+
126
+ def map_to_a
127
+ map{|item| [item]}
128
+ end
129
+
130
+
131
+ # Map each item => item.to_f
132
+ #
133
+ # @return [Enumerable<Float>] a list of each item.to_f
134
+ #
135
+ # @example
136
+ # strings = ["1","2","3"]
137
+ # strings.map_to_f => [1.0, 2.0, 3.0]
138
+ #
139
+ # A typical use is to convert a list of String items to a list of float items.
140
+ #
141
+ # This method is a fast way to get the same results as items.map(&:to_f)
142
+
143
+ def map_to_f
144
+ map{|item| item.to_f}
145
+ end
146
+
147
+
148
+ # Map each item => item.to_i
149
+ #
150
+ # @return [Enumerable<Integer>] a list of each item.to_i
151
+ #
152
+ # @example
153
+ # strings = ["1","2","3"]
154
+ # strings.map_to_i => [1, 2, 3]
155
+ #
156
+ # A typical use is to convert a list of String items to a list of integer items.
157
+ #
158
+ # This method is a fast way to get the same results as items.map(&:to_i)
159
+
160
+ def map_to_i
161
+ map{|item| item.to_i}
162
+ end
163
+
164
+
165
+ # Map each item => item.to_s
166
+ #
167
+ # @return [Enumerable<String>] a list of each item.to_s
168
+ #
169
+ # @example
170
+ # numbers = [1, 2, 3]
171
+ # numbers.map_to_s => ["1", "2", "3"]
172
+ #
173
+ # A typical use is to convert a list of Numeric items to a list of String items.
174
+ #
175
+ # This method is a fast way to get the same results as items.map(&:to_s)
176
+
177
+ def map_to_s
178
+ map{|item| item.to_s}
179
+ end
180
+
181
+
182
+ # Map each item => item.to_sym
183
+ #
184
+ # @return [Enumerable<Symbol>] a list of each item.to_sym
185
+ #
186
+ # @example
187
+ # strings = ["foo", "goo", "hoo"]
188
+ # strings.map_to_sym => [:foo, :goo, :hoo]
189
+ #
190
+ # A typical use is to convert a list of Object items to a list of Symbol items.
191
+ #
192
+ # This method is a fast way to get the same results as items.map(&:to_sym)
193
+
194
+ def map_to_sym
195
+ map{|item| item.to_sym}
196
+ end
197
+
198
+
199
+ # Map each item and its index => a new output
200
+ #
201
+ # @return [Enumerable<Object>] an list of each item transformed by the block
202
+ # @see Enumerable#map
203
+ # @see Enumerable#each_with_index
204
+ #
205
+ # @example
206
+ # strings = ["a", "b", "c"]
207
+ # strings.map_with_index{|string,index| "#{string}#{index}"}
208
+ # => ["a0, "b1", "c3"]
209
+
210
+ def map_with_index
211
+ index=-1
212
+ map{|item| index+=1; yield(item,index)}
213
+ end
214
+
215
+
216
+ ########################################################################
217
+ #
218
+ # select
219
+ #
220
+ ########################################################################
221
+
222
+
223
+ # @example
224
+ # enum.select_while {|obj| block }
225
+ # => array
226
+ #
227
+ # @return [Array<Object>] the leading elements for which block is not false or nil.
228
+
229
+ def select_while
230
+ arr = []
231
+ each{|item| yield(item) ? (arr << item) : break}
232
+ return arr
233
+ end
234
+
235
+
236
+ # @example
237
+ # enum.select_until {|obj| block }
238
+ # => array
239
+ #
240
+ # @return [Array<Object>] the leading elements for which block is false or nil.
241
+
242
+ def select_until
243
+ arr = []
244
+ each{|item| yield(item) ? break : (arr << item)}
245
+ return arr
246
+ end
247
+
248
+
249
+ # Calls block with two arguments, the item and its index, for each item in enum.
250
+ #
251
+ # @example
252
+ # enum.select_with_index {|obj,i| block }
253
+ # => array
254
+ #
255
+ # @return [Array<Object> the leading elements for which block is not false or nil.
256
+
257
+ def select_with_index
258
+ index = 0
259
+ arr = []
260
+ each{|item|
261
+ if yield(item,index)
262
+ arr << item
263
+ index+=1
264
+ else
265
+ break
266
+ end
267
+ }
268
+ return arr
269
+ end
270
+
271
+
272
+ ########################################################################
273
+ #
274
+ # bisect
275
+ #
276
+ ########################################################################
277
+
278
+ # @example
279
+ # enum.bisect {|obj| block}
280
+ # => array of positives, array of negatives
281
+ #
282
+ # @return [Array<Array<Object>] an array of two arrays:
283
+ # the first array is the elements for which block is true,
284
+ # the second array is the elements for which block is false or nil.
285
+
286
+ def bisect
287
+ accept=[]
288
+ reject=[]
289
+ each{|item|
290
+ if yield(item)
291
+ accept << item
292
+ else
293
+ reject << item
294
+ end
295
+ }
296
+ return accept,reject
297
+ end
298
+
299
+
300
+ ########################################################################
301
+ #
302
+ # mutually exclusive?
303
+ #
304
+ ########################################################################
305
+
306
+ # @example
307
+ # enum.mutex? {|obj| block}
308
+ # => true iff block is not false or nil, zero or one time
309
+ #
310
+ # @return boolean true iff block is not false or nil, zero or one time
311
+
312
+ def mutex?
313
+ num = 0
314
+ each{|item|
315
+ if yield(item)
316
+ num += 1
317
+ if num > 1 then return false end
318
+ end
319
+ }
320
+ return true
321
+ end
322
+
323
+
324
+ ########################################################################
325
+ #
326
+ # nitems
327
+ #
328
+ ########################################################################
329
+
330
+
331
+ # @example
332
+ # enum.nitems?(n) {| obj | block }
333
+ # => true iff the block is not false or nil num times
334
+ #
335
+ # @return [Boolean] true iff the block is not false or nil num times
336
+
337
+ def nitems?(n)
338
+ num = 0
339
+ each{|item|
340
+ if yield(item)
341
+ num+=1
342
+ if num > n then return false end
343
+ end
344
+ }
345
+ return num==n
346
+ end
347
+
348
+
349
+ # @example
350
+ # enum.nitems_while {| obj | block }
351
+ # => number of items
352
+ #
353
+ # @return [Integer] the number of leading elements for which block is not false or nil.
354
+
355
+ def nitems_while
356
+ num = 0
357
+ each{|item| yield(item) ? (num+=1) : break}
358
+ return num
359
+ end
360
+
361
+
362
+ # @example
363
+ # enum.nitems_until {| obj | block }
364
+ # => number of items
365
+ #
366
+ # @return [Integer] the number of leading elements for which block is false.
367
+
368
+ def nitems_until
369
+ num = 0
370
+ each{|item|
371
+ if yield(item)
372
+ break
373
+ else
374
+ num+=1
375
+ end
376
+ }
377
+ return num
378
+ end
379
+
380
+
381
+ # Calls block with two arguments, the item and its index, for each item in enum.
382
+ #
383
+ # @example
384
+ # enum.nitems_with_index {|obj,i| block }
385
+ # => number of items
386
+ #
387
+ # @return [Integer] the number of leading elements for which block is true.
388
+
389
+ def nitems_with_index
390
+ index = 0
391
+ each{|item| yield(item,index) ? (index+=1) : break}
392
+ return index
393
+ end
394
+
395
+
396
+ # Shortcut to Array#join to concatenate the items into a string
397
+ #
398
+ # @example
399
+ # ["foo", "goo", "hoo"].join
400
+ # => "foogoohoo"
401
+ #
402
+ # @return [String] concatenated string
403
+ #
404
+ # @see Array#join
405
+
406
+ def join(*op)
407
+ to_a.join(*op)
408
+ end
409
+
410
+
411
+ ########################################################################
412
+ #
413
+ # set math
414
+ #
415
+ ########################################################################
416
+
417
+
418
+ # @return [Boolean] true if this _enum_ intersects another _enum_.
419
+ #
420
+ # A developer may want to optimize this implementation for
421
+ # other classes, such as detecting whether a range intersects
422
+ # another range simply by comparing the ranges' min/max values.
423
+ #
424
+ # @example
425
+ # ['a','b','c'].intersect?(['c','d','e'] => true
426
+ # ['a','b','c'].intersect?(['d','e','f'] => false
427
+ #
428
+ def intersect?(enum)
429
+ return enum.any?{|item| self.include?(item)}
430
+ end
431
+
432
+
433
+ # @return [Array] the cartesian product of the enumerations.
434
+ #
435
+ # @see http://en.wikipedia.org/wiki/Cartesian_product
436
+ #
437
+ # This is the fastest implementation we have found.
438
+ # It returns results in typical order.
439
+ #
440
+ # @author Thomas Hafner
441
+ # @see http://www.ruby-forum.com/topic/95519
442
+ #
443
+ # For our benchmarks, we also compared these:
444
+ # - By William James, http://www.ruby-forum.com/topic/95519
445
+ # - By Brian Schröäer, http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151857
446
+
447
+ def self.cartesian_product(*enums)
448
+ result = [[]]
449
+ while [] != enums
450
+ t, result = result, []
451
+ b, *enums = enums
452
+ t.each do |a|
453
+ b.each do |n|
454
+ result << a + [n]
455
+ end
456
+ end
457
+ end
458
+ result
459
+ end
460
+
461
+
462
+ def cartesian_product(*enums)
463
+ Enumerable.cartesian_product(self,*enums)
464
+ end
465
+
466
+
467
+ # Calculate the power set.
468
+ #
469
+ # @return [Array<Array<Object>>] the power set: an array with all subsets of the enum's elements.
470
+ # @see http://en.wikipedia.org/wiki/Power_set
471
+ #
472
+ # This implementation is from the blog post below:
473
+ # @see http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
474
+ #
475
+ # @example
476
+ # [1,2,3].power_set.sort
477
+ # => [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
478
+
479
+ def power_set
480
+ inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
481
+ end
482
+
483
+ end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # File extensions
4
+
5
+ class File
6
+
7
+ # @return [String] File.join(File.dirname(dirname),strings)
8
+ #
9
+ # @example
10
+ # File.joindir(__FILE__,'foo.txt')
11
+ # => '/home/john/foo.txt'
12
+
13
+ def File.joindir(dirname,*strings)
14
+ File.join(File.dirname(dirname),strings)
15
+ end
16
+
17
+ end
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Fixnum extensions
4
+
5
+ class Fixnum
6
+
7
+
8
+ # @return true if the number is even
9
+ #
10
+ # @example
11
+ # 2.even? => true
12
+ # 3.even? => false
13
+ #
14
+ # From http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4516
15
+
16
+ def even?
17
+ return self & 1 == 0
18
+ end
19
+
20
+
21
+ # @return true if the number is odd
22
+ #
23
+ # @example
24
+ # 2.odd? => false
25
+ # 3.odd? => true
26
+ #
27
+ # From http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4516
28
+
29
+ def odd?
30
+ return self & 1 != 0
31
+ end
32
+
33
+
34
+ end