webget_ruby_ramp 1.8.0 → 1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +2 -2
- data/README.rdoc +24 -7
- data/lib/webget_ruby_ramp.rb +2 -2
- data/lib/webget_ruby_ramp/array.rb +56 -43
- data/lib/webget_ruby_ramp/class.rb +3 -1
- data/lib/webget_ruby_ramp/csv.rb +4 -4
- data/lib/webget_ruby_ramp/date.rb +12 -12
- data/lib/webget_ruby_ramp/enumerable.rb +101 -46
- data/lib/webget_ruby_ramp/file.rb +2 -2
- data/lib/webget_ruby_ramp/fixnum.rb +4 -7
- data/lib/webget_ruby_ramp/hash.rb +18 -15
- data/lib/webget_ruby_ramp/integer.rb +8 -7
- data/lib/webget_ruby_ramp/io.rb +24 -20
- data/lib/webget_ruby_ramp/kernel.rb +32 -0
- data/lib/webget_ruby_ramp/math.rb +10 -2
- data/lib/webget_ruby_ramp/nil.rb +5 -2
- data/lib/webget_ruby_ramp/numeric.rb +5 -71
- data/lib/webget_ruby_ramp/object.rb +4 -3
- data/lib/webget_ruby_ramp/process.rb +54 -7
- data/lib/webget_ruby_ramp/string.rb +54 -24
- data/lib/webget_ruby_ramp/symbol.rb +16 -0
- data/lib/webget_ruby_ramp/time.rb +19 -11
- data/lib/webget_ruby_ramp/xml.rb +30 -20
- data/lib/webget_ruby_ramp/yaml.rb +5 -4
- data/test/webget_ruby_ramp/numeric_test.rb +0 -60
- data/test/webget_ruby_ramp/string_test.rb +0 -5
- metadata +2 -2
- metadata.gz.sig +1 -1
@@ -13,14 +13,16 @@ module Enumerable
|
|
13
13
|
|
14
14
|
# Convert an enumerable to a hash.
|
15
15
|
#
|
16
|
-
#
|
16
|
+
# @return [Hash<Object,Object>] a hash of the enumerable's items
|
17
|
+
#
|
18
|
+
# @example
|
17
19
|
# array=[[:a, :b],[:c, :d],[:e, :f]]
|
18
20
|
# array.to_h => {:a=>:b, :c=>:d, :e=>:f}
|
19
21
|
#
|
20
22
|
# If a key occurs more than once, then this will automatically
|
21
23
|
# convert the value to an array of the keys' values.
|
22
24
|
#
|
23
|
-
#
|
25
|
+
# @example
|
24
26
|
# array=[[:a,:b],[:a,:c],[:a,:d]]
|
25
27
|
# array.to_h => {:a=>[:b, :c, :d]}
|
26
28
|
|
@@ -44,9 +46,11 @@ module Enumerable
|
|
44
46
|
end
|
45
47
|
|
46
48
|
|
47
|
-
# Convert the enumerable to a hash by mapping each item to a
|
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
|
48
52
|
#
|
49
|
-
#
|
53
|
+
# @example
|
50
54
|
# strings = ["red","blue","green"]
|
51
55
|
# strings.index_by{|a| a.size]}
|
52
56
|
# => {3 => "red", 4 => "blue", 5 => "green"}
|
@@ -55,21 +59,23 @@ module Enumerable
|
|
55
59
|
#
|
56
60
|
# From http://stackoverflow.com/questions/412771/cleanest-way-to-create-a-hash-from-an-array
|
57
61
|
#
|
58
|
-
#
|
62
|
+
# @see #hash_by
|
59
63
|
|
60
64
|
def index_by
|
61
65
|
inject({}) {|hash, elem| hash.merge!(yield(elem) => elem) }
|
62
66
|
end
|
63
67
|
|
64
68
|
|
65
|
-
# Convert the enumerable to a hash by mapping each item to a
|
69
|
+
# Convert the enumerable to a hash by mapping each item to a pair [item, new item]
|
66
70
|
#
|
67
|
-
#
|
71
|
+
# @return [Hash<Object,Object>] a hash of the enumerable's items
|
72
|
+
#
|
73
|
+
# @example
|
68
74
|
# strings = ["red","blue","green"]
|
69
75
|
# strings.hash_by{|a| [a.size, a.upcase]}
|
70
76
|
# => {3 => "RED", 4 => "BLUE", 5 => "GREEN"}
|
71
77
|
#
|
72
|
-
#
|
78
|
+
# @see #index_by
|
73
79
|
|
74
80
|
def hash_by
|
75
81
|
map{|item| yield(item)}.to_h
|
@@ -86,7 +92,9 @@ module Enumerable
|
|
86
92
|
|
87
93
|
# Map each item => item.id
|
88
94
|
#
|
89
|
-
#
|
95
|
+
# @return [Enumerable<Object>] an list of each item.id
|
96
|
+
#
|
97
|
+
# @example
|
90
98
|
# users = User.find(:all)
|
91
99
|
# users.map_id => [1,2,3,4,...]
|
92
100
|
#
|
@@ -101,7 +109,9 @@ module Enumerable
|
|
101
109
|
|
102
110
|
# Map each item => item.to_a
|
103
111
|
#
|
104
|
-
#
|
112
|
+
# @return [Enumberable<Array<Object>>] a list of each item.to_a
|
113
|
+
#
|
114
|
+
# @example
|
105
115
|
# set1 = Set.new([:a,:b,:c])
|
106
116
|
# set2 = Set.new([:d,:e,:f])
|
107
117
|
# set3 = Set.new([:g,:h,:i])
|
@@ -120,7 +130,9 @@ module Enumerable
|
|
120
130
|
|
121
131
|
# Map each item => item.to_f
|
122
132
|
#
|
123
|
-
#
|
133
|
+
# @return [Enumerable<Float>] a list of each item.to_f
|
134
|
+
#
|
135
|
+
# @example
|
124
136
|
# strings = ["1","2","3"]
|
125
137
|
# strings.map_to_f => [1.0, 2.0, 3.0]
|
126
138
|
#
|
@@ -135,7 +147,9 @@ module Enumerable
|
|
135
147
|
|
136
148
|
# Map each item => item.to_i
|
137
149
|
#
|
138
|
-
#
|
150
|
+
# @return [Enumerable<Integer>] a list of each item.to_i
|
151
|
+
#
|
152
|
+
# @example
|
139
153
|
# strings = ["1","2","3"]
|
140
154
|
# strings.map_to_i => [1, 2, 3]
|
141
155
|
#
|
@@ -150,7 +164,9 @@ module Enumerable
|
|
150
164
|
|
151
165
|
# Map each item => item.to_s
|
152
166
|
#
|
153
|
-
#
|
167
|
+
# @return [Enumerable<String>] a list of each item.to_s
|
168
|
+
#
|
169
|
+
# @example
|
154
170
|
# numbers = [1, 2, 3]
|
155
171
|
# numbers.map_to_s => ["1", "2", "3"]
|
156
172
|
#
|
@@ -165,7 +181,9 @@ module Enumerable
|
|
165
181
|
|
166
182
|
# Map each item => item.to_sym
|
167
183
|
#
|
168
|
-
#
|
184
|
+
# @return [Enumerable<Symbol>] a list of each item.to_sym
|
185
|
+
#
|
186
|
+
# @example
|
169
187
|
# strings = ["foo", "goo", "hoo"]
|
170
188
|
# strings.map_to_sym => [:foo, :goo, :hoo]
|
171
189
|
#
|
@@ -180,10 +198,11 @@ module Enumerable
|
|
180
198
|
|
181
199
|
# Map each item and its index => a new output
|
182
200
|
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
201
|
+
# @return [Enumerable<Object>] an list of each item transformed by the block
|
202
|
+
# @see Enumerable#map
|
203
|
+
# @see Enumerable#each_with_index
|
186
204
|
#
|
205
|
+
# @example
|
187
206
|
# strings = ["a", "b", "c"]
|
188
207
|
# strings.map_with_index{|string,index| "#{string}#{index}"}
|
189
208
|
# => ["a0, "b1", "c3"]
|
@@ -201,8 +220,11 @@ module Enumerable
|
|
201
220
|
########################################################################
|
202
221
|
|
203
222
|
|
204
|
-
#
|
205
|
-
#
|
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.
|
206
228
|
|
207
229
|
def select_while
|
208
230
|
arr = []
|
@@ -211,8 +233,11 @@ module Enumerable
|
|
211
233
|
end
|
212
234
|
|
213
235
|
|
214
|
-
#
|
215
|
-
#
|
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.
|
216
241
|
|
217
242
|
def select_until
|
218
243
|
arr = []
|
@@ -221,9 +246,13 @@ module Enumerable
|
|
221
246
|
end
|
222
247
|
|
223
248
|
|
224
|
-
# enum.select_with_index {|obj,i| block } => array
|
225
249
|
# Calls block with two arguments, the item and its index, for each item in enum.
|
226
|
-
#
|
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.
|
227
256
|
|
228
257
|
def select_with_index
|
229
258
|
index = 0
|
@@ -246,9 +275,13 @@ module Enumerable
|
|
246
275
|
#
|
247
276
|
########################################################################
|
248
277
|
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
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.
|
252
285
|
|
253
286
|
def bisect
|
254
287
|
accept=[]
|
@@ -271,8 +304,11 @@ module Enumerable
|
|
271
304
|
########################################################################
|
272
305
|
|
273
306
|
|
274
|
-
#
|
275
|
-
#
|
307
|
+
# @example
|
308
|
+
# enum.nitems_while {| obj | block }
|
309
|
+
# => number of items
|
310
|
+
#
|
311
|
+
# @return [Integer] the number of leading elements for which block is not false or nil.
|
276
312
|
|
277
313
|
def nitems_while
|
278
314
|
num = 0
|
@@ -281,8 +317,11 @@ module Enumerable
|
|
281
317
|
end
|
282
318
|
|
283
319
|
|
284
|
-
#
|
285
|
-
#
|
320
|
+
# @example
|
321
|
+
# enum.nitems_until {| obj | block }
|
322
|
+
# => number of items
|
323
|
+
#
|
324
|
+
# @return [Integer] the number of leading elements for which block is false.
|
286
325
|
|
287
326
|
def nitems_until
|
288
327
|
num = 0
|
@@ -297,9 +336,13 @@ module Enumerable
|
|
297
336
|
end
|
298
337
|
|
299
338
|
|
300
|
-
# enum.nitems_with_index {|obj,i| block } => number of items
|
301
339
|
# Calls block with two arguments, the item and its index, for each item in enum.
|
302
|
-
#
|
340
|
+
#
|
341
|
+
# @example
|
342
|
+
# enum.nitems_with_index {|obj,i| block }
|
343
|
+
# => number of items
|
344
|
+
#
|
345
|
+
# @return [Integer] the number of leading elements for which block is true.
|
303
346
|
|
304
347
|
def nitems_with_index
|
305
348
|
index = 0
|
@@ -309,6 +352,14 @@ module Enumerable
|
|
309
352
|
|
310
353
|
|
311
354
|
# Shortcut to Array#join to concatenate the items into a string
|
355
|
+
#
|
356
|
+
# @example
|
357
|
+
# ["foo", "goo", "hoo"].join
|
358
|
+
# => "foogoohoo"
|
359
|
+
#
|
360
|
+
# @return [String] concatenated string
|
361
|
+
#
|
362
|
+
# @see Array#join
|
312
363
|
|
313
364
|
def join(*op)
|
314
365
|
to_a.join(*op)
|
@@ -322,14 +373,14 @@ module Enumerable
|
|
322
373
|
########################################################################
|
323
374
|
|
324
375
|
|
325
|
-
#
|
376
|
+
# @return [Boolean] true if this _enum_ intersects another _enum_.
|
326
377
|
#
|
327
|
-
#
|
378
|
+
# This implementation uses #to_a and array intersection.
|
328
379
|
# A developer may want to optimize this implementation for
|
329
380
|
# other classes, such as detecting whether a range intersects
|
330
381
|
# another range simply by comparing the ranges' min/max values.
|
331
382
|
#
|
332
|
-
#
|
383
|
+
# @example
|
333
384
|
# ['a','b','c'].intersect?(['c','d','e'] => true
|
334
385
|
# ['a','b','c'].intersect?(['d','e','f'] => false
|
335
386
|
|
@@ -338,19 +389,20 @@ module Enumerable
|
|
338
389
|
end
|
339
390
|
|
340
391
|
|
341
|
-
#
|
342
|
-
#
|
392
|
+
# @return [Array] the cartesian product of the enumerations.
|
393
|
+
#
|
394
|
+
# @see http://en.wikipedia.org/wiki/Cartesian_product
|
343
395
|
#
|
344
396
|
# This is the fastest implementation we have found.
|
345
397
|
# It returns results in typical order.
|
346
398
|
#
|
347
|
-
#
|
348
|
-
#
|
399
|
+
# @author Thomas Hafner
|
400
|
+
# @see http://www.ruby-forum.com/topic/95519
|
349
401
|
#
|
350
|
-
# For our benchmarks, we also compared
|
402
|
+
# For our benchmarks, we also compared these:
|
351
403
|
# - By William James, http://www.ruby-forum.com/topic/95519
|
352
404
|
# - By Brian Schröäer, http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151857
|
353
|
-
|
405
|
+
|
354
406
|
def self.cartesian_product(*enums)
|
355
407
|
result = [[]]
|
356
408
|
while [] != enums
|
@@ -365,18 +417,21 @@ module Enumerable
|
|
365
417
|
result
|
366
418
|
end
|
367
419
|
|
420
|
+
|
368
421
|
def cartesian_product(*enums)
|
369
422
|
Enumerable.cartesian_product(self,*enums)
|
370
423
|
end
|
371
424
|
|
372
425
|
|
373
|
-
#
|
374
|
-
#
|
426
|
+
# Calculate the power set.
|
427
|
+
#
|
428
|
+
# @return [Array<Array<Object>>] the power set: an array with all subsets of the enum's elements.
|
429
|
+
# @see http://en.wikipedia.org/wiki/Power_set
|
375
430
|
#
|
376
|
-
# This implementation is from
|
377
|
-
# http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
|
431
|
+
# This implementation is from the blog post below:
|
432
|
+
# @see http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
|
378
433
|
#
|
379
|
-
#
|
434
|
+
# @example
|
380
435
|
# [1,2,3].power_set.sort
|
381
436
|
# => [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
|
382
437
|
|
@@ -5,9 +5,9 @@
|
|
5
5
|
class Fixnum
|
6
6
|
|
7
7
|
|
8
|
-
#
|
8
|
+
# @return true if the number is even
|
9
9
|
#
|
10
|
-
#
|
10
|
+
# @example
|
11
11
|
# 2.even? => true
|
12
12
|
# 3.even? => false
|
13
13
|
#
|
@@ -18,15 +18,12 @@ class Fixnum
|
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
|
-
#
|
21
|
+
# @return true if the number is odd
|
22
22
|
#
|
23
|
-
#
|
23
|
+
# @example
|
24
24
|
# 2.odd? => false
|
25
25
|
# 3.odd? => true
|
26
26
|
#
|
27
|
-
# n.b. we test to see if this method already exists,
|
28
|
-
# because this method is defined in Ruby 1.8.7 onward.
|
29
|
-
#
|
30
27
|
# From http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/4516
|
31
28
|
|
32
29
|
def odd?
|
@@ -7,7 +7,8 @@ require 'yaml'
|
|
7
7
|
class Hash
|
8
8
|
|
9
9
|
|
10
|
-
#
|
10
|
+
# @return [Boolean] true if size > 0
|
11
|
+
|
11
12
|
def size?
|
12
13
|
size>0
|
13
14
|
end
|
@@ -17,13 +18,13 @@ class Hash
|
|
17
18
|
#
|
18
19
|
# The keys are sorted.
|
19
20
|
#
|
20
|
-
#
|
21
|
+
# @example
|
21
22
|
# h = { "xyz" => "123", "abc" => "789" }
|
22
23
|
# h.each_sort {|key, val| ... }
|
23
24
|
# => calls the block with "abc" => "789", then with "xyz" => "123"
|
24
25
|
|
25
26
|
def each_sort
|
26
|
-
|
27
|
+
keys.sort.each{|key| yield key,self[key] }
|
27
28
|
end
|
28
29
|
|
29
30
|
|
@@ -31,12 +32,12 @@ class Hash
|
|
31
32
|
# passing the key as a parameter,
|
32
33
|
# and updating it in place.
|
33
34
|
#
|
34
|
-
#
|
35
|
+
# @example
|
35
36
|
# h = { "a" => "b", "c" => "d" }
|
36
37
|
# h.each_key! {|key| key.upcase }
|
37
38
|
# h => { "A" => "b", "C" => "d" }
|
38
39
|
#
|
39
|
-
#
|
40
|
+
# @return self
|
40
41
|
|
41
42
|
def each_key!
|
42
43
|
each_pair{|key,value|
|
@@ -48,19 +49,21 @@ class Hash
|
|
48
49
|
self[key2]=value
|
49
50
|
end
|
50
51
|
}
|
52
|
+
return self
|
51
53
|
end
|
52
54
|
|
53
55
|
|
56
|
+
|
54
57
|
# Calls block once for each key in hsh,
|
55
58
|
# passing the key and value as parameters,
|
56
59
|
# and updated them in place.
|
57
60
|
#
|
58
|
-
#
|
61
|
+
# @example
|
59
62
|
# h = { "a" => "b", "c" => "d" }
|
60
63
|
# h.each_pair! {|key,value| key.upcase, value.upcase }
|
61
64
|
# h => { "A" => "B", "C" => "D" }
|
62
65
|
#
|
63
|
-
#
|
66
|
+
# @return self.
|
64
67
|
|
65
68
|
def each_pair!
|
66
69
|
each_pair{|key,value|
|
@@ -84,12 +87,12 @@ class Hash
|
|
84
87
|
# passing the value as a parameter,
|
85
88
|
# and updating it in place.
|
86
89
|
#
|
87
|
-
#
|
90
|
+
# @example
|
88
91
|
# h = { "a" => "b", "c" => "d" }
|
89
92
|
# h.each_value! {|value| value.upcase }
|
90
93
|
# h => { "a" => "B", "c" => "d" }
|
91
94
|
#
|
92
|
-
#
|
95
|
+
# @return self.
|
93
96
|
|
94
97
|
def each_value!
|
95
98
|
each_pair{|key,value|
|
@@ -107,7 +110,7 @@ class Hash
|
|
107
110
|
# Calls block once for each key-value pair in hsh,
|
108
111
|
# passing the key and value as paramters to the block.
|
109
112
|
#
|
110
|
-
#
|
113
|
+
# @example
|
111
114
|
# h = {"a"=>"b", "c"=>"d", "e"=>"f" }
|
112
115
|
# h.map_pair{|key,value| key+value }
|
113
116
|
# => ["ab","cd","ef"]
|
@@ -121,7 +124,7 @@ class Hash
|
|
121
124
|
#
|
122
125
|
# From http://snippets.dzone.com/tag/yaml
|
123
126
|
#
|
124
|
-
#
|
127
|
+
# @example
|
125
128
|
# h = {"a"=>"b", "c"=>"d", "e"=>"f" }
|
126
129
|
# h.to_yaml_sort
|
127
130
|
# => "--- \na: b\nc: d\ne: f\n"
|
@@ -179,13 +182,13 @@ class Hash
|
|
179
182
|
#
|
180
183
|
# The pivot method is especially useful for calculating subtotals.
|
181
184
|
#
|
182
|
-
#
|
185
|
+
# @example
|
183
186
|
# r = h.pivot(:keys)
|
184
187
|
# r['a'].sum => 6
|
185
188
|
# r['b'].sum => 15
|
186
189
|
# r['c'].sum => 24
|
187
190
|
#
|
188
|
-
#
|
191
|
+
# @example
|
189
192
|
# r=h.pivot(:vals)
|
190
193
|
# r['x'].sum => 12
|
191
194
|
# r['y'].sum => 15
|
@@ -195,12 +198,12 @@ class Hash
|
|
195
198
|
#
|
196
199
|
# You can provide a block that will be called for the pivot items.
|
197
200
|
#
|
198
|
-
#
|
201
|
+
# @example
|
199
202
|
# h.pivot(:keys){|items| items.max } => {"a"=>3,"b"=>6,"c"=>9}
|
200
203
|
# h.pivot(:keys){|items| items.join("/") } => {"a"=>"1/2/3","b"=>"4/5/6","c"=>"7/8/9"}
|
201
204
|
# h.pivot(:keys){|items| items.inject{|sum,x| sum+=x } } => {"a"=>6,"b"=>15,"c"=>24}
|
202
205
|
#
|
203
|
-
#
|
206
|
+
# @example
|
204
207
|
# h.pivot(:vals){|items| items.max } => {"a"=>7,"b"=>8,"c"=>9}
|
205
208
|
# h.pivot(:vals){|items| items.join("-") } => {"a"=>"1-4-7","b"=>"2-5-8","c"=>"3-6-9"}
|
206
209
|
# h.pivot(:vals){|items| items.inject{|sum,x| sum+=x } } => {"a"=>12,"b"=>15,"c"=>18}
|