webget_ruby_ramp 1.7.3 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data.tar.gz.sig +1 -1
- data/LICENSE.txt +12 -0
- data/README.rdoc +226 -0
- data/lib/webget_ruby_ramp.rb +10 -2
- data/lib/webget_ruby_ramp/array.rb +2 -0
- data/lib/webget_ruby_ramp/class.rb +36 -0
- data/lib/webget_ruby_ramp/csv.rb +8 -3
- data/lib/webget_ruby_ramp/date.rb +17 -13
- data/lib/webget_ruby_ramp/enumerable.rb +2 -0
- data/lib/webget_ruby_ramp/file.rb +2 -0
- data/lib/webget_ruby_ramp/hash.rb +13 -1
- data/lib/webget_ruby_ramp/integer.rb +24 -0
- data/lib/webget_ruby_ramp/io.rb +2 -0
- data/lib/webget_ruby_ramp/kernel.rb +2 -0
- data/lib/webget_ruby_ramp/math.rb +2 -0
- data/lib/webget_ruby_ramp/nil.rb +2 -0
- data/lib/webget_ruby_ramp/numeric.rb +5 -6
- data/lib/webget_ruby_ramp/object.rb +2 -0
- data/lib/webget_ruby_ramp/process.rb +3 -1
- data/lib/webget_ruby_ramp/string.rb +2 -0
- data/lib/webget_ruby_ramp/symbol.rb +2 -0
- data/lib/webget_ruby_ramp/time.rb +2 -0
- data/lib/webget_ruby_ramp/xml.rb +2 -0
- data/lib/webget_ruby_ramp/yaml.rb +2 -0
- data/test/webget_ruby_ramp/array_test.rb +218 -75
- data/test/webget_ruby_ramp/class_test.rb +76 -0
- data/test/webget_ruby_ramp/csv_test.rb +35 -8
- data/test/webget_ruby_ramp/date_test.rb +14 -2
- data/test/webget_ruby_ramp/enumerable_test.rb +2 -2
- data/test/webget_ruby_ramp/hash_test.rb +91 -4
- data/test/webget_ruby_ramp/integer_test.rb +16 -0
- data/test/webget_ruby_ramp/math_test.rb +1 -0
- data/test/webget_ruby_ramp/numeric_test.rb +60 -0
- metadata +6 -2
- metadata.gz.sig +0 -0
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
# Integer extensions
|
2
4
|
|
3
5
|
class Integer
|
@@ -19,4 +21,26 @@ class Integer
|
|
19
21
|
(0...self).map{|item| yield item}
|
20
22
|
end
|
21
23
|
|
24
|
+
|
25
|
+
# Return true if the number is even
|
26
|
+
#
|
27
|
+
# ==Example
|
28
|
+
# 2.even? => true
|
29
|
+
# 3.even? => false
|
30
|
+
|
31
|
+
def even?
|
32
|
+
self % 2 == 0
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# Return true if the number is odd
|
37
|
+
#
|
38
|
+
# ==Example
|
39
|
+
# 2.odd? => false
|
40
|
+
# 3.odd? => true
|
41
|
+
|
42
|
+
def odd?
|
43
|
+
self %2 == 1
|
44
|
+
end
|
45
|
+
|
22
46
|
end
|
data/lib/webget_ruby_ramp/io.rb
CHANGED
data/lib/webget_ruby_ramp/nil.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
# Numeric extensions
|
2
4
|
|
3
5
|
class Numeric
|
4
6
|
|
5
|
-
|
6
7
|
# Return 0 if the given flag is any of: nil, false, 0, [], {}
|
7
8
|
#
|
8
9
|
# ==Example
|
@@ -11,8 +12,7 @@ class Numeric
|
|
11
12
|
#
|
12
13
|
|
13
14
|
def if(flag)
|
14
|
-
|
15
|
-
return (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? 0 : self
|
15
|
+
if [nil,false,0,[],{}].include?(flag) then 0 else self end
|
16
16
|
end
|
17
17
|
|
18
18
|
|
@@ -23,8 +23,7 @@ class Numeric
|
|
23
23
|
# 3.unless(false) => 3
|
24
24
|
|
25
25
|
def unless(flag)
|
26
|
-
|
27
|
-
return (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? self : 0
|
26
|
+
if [nil,false,0,[],{}].include?(flag) then self else 0 end
|
28
27
|
end
|
29
28
|
|
30
29
|
|
@@ -51,7 +50,7 @@ class Numeric
|
|
51
50
|
|
52
51
|
# Return self / 10^6
|
53
52
|
def mega
|
54
|
-
self/
|
53
|
+
self/1000000
|
55
54
|
end
|
56
55
|
|
57
56
|
# Return self / 10^3
|
data/lib/webget_ruby_ramp/xml.rb
CHANGED
@@ -4,98 +4,171 @@ require 'webget_ruby_ramp'
|
|
4
4
|
|
5
5
|
class ArrayTest < Test::Unit::TestCase
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
assert_equal('
|
13
|
-
|
14
|
-
|
7
|
+
A=['a','b','c','d']
|
8
|
+
B=['w','x','y','z']
|
9
|
+
SLICEABLE=['a','b','c','d','e','f','g','h']
|
10
|
+
|
11
|
+
def test_join_with_blank
|
12
|
+
assert_equal('abcd', A.join(''))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_join_with_infix
|
16
|
+
assert_equal('a*b*c*d', A.join('*'))
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
18
|
-
assert_equal(
|
19
|
-
|
19
|
+
def test_join_with_infix_and_prefix_and_suffix
|
20
|
+
assert_equal('[a]*[b]*[c]*[d]', A.join('*','[',']'))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_join_with_prefix_and_suffix
|
24
|
+
assert_equal('[a][b][c][d]', A.join('[',']'))
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_join_with_no_ops
|
28
|
+
assert_equal('abcd',A.join)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_join_with_too_many_ops
|
32
|
+
assert_raise(ArgumentError){ A.join('','','','') }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_size_with_one_item
|
20
36
|
assert_equal(true,[1].size?)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_size_with_two_items
|
21
40
|
assert_equal(true,[1,2].size?)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_size_with_empty
|
44
|
+
assert_equal(false,[].size?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_size_with_nil
|
48
|
+
assert_equal(true,[nil].size?)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_size_with_one_empty
|
22
52
|
assert_equal(true,[[]].size?)
|
23
53
|
end
|
24
54
|
|
25
55
|
def test_rotate
|
26
|
-
a=
|
56
|
+
a=A.dup
|
57
|
+
a.rotate!
|
58
|
+
assert_equal(['b','c','d','a'],a)
|
27
59
|
a.rotate!
|
28
|
-
assert_equal([
|
60
|
+
assert_equal(['c','d','a','b'],a)
|
29
61
|
a.rotate!
|
30
|
-
assert_equal([
|
62
|
+
assert_equal(['d','a','b','c'],a)
|
31
63
|
a.rotate!
|
64
|
+
assert_equal(['a','b','c','d'],a)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_rotate_with_empty
|
32
68
|
a=[]
|
33
69
|
a.rotate!
|
34
70
|
assert_equal([],a)
|
35
71
|
end
|
36
72
|
|
37
73
|
def test_choice
|
38
|
-
a=[1,2,3,4]
|
39
74
|
5.times {
|
40
|
-
c=
|
41
|
-
assert_equal(
|
42
|
-
assert(
|
75
|
+
c=A.choice
|
76
|
+
assert_equal(String,c.class)
|
77
|
+
assert(A.include?(c))
|
43
78
|
}
|
44
79
|
end
|
45
80
|
|
46
81
|
def test_choices
|
47
|
-
a=[1,2,3,4]
|
48
82
|
5.times {
|
49
|
-
c=
|
83
|
+
c=A.choices(2)
|
50
84
|
assert_equal(2,c.size)
|
51
|
-
assert(
|
52
|
-
assert(
|
53
|
-
c=
|
85
|
+
assert(A.include?(c[0]))
|
86
|
+
assert(A.include?(c[1]))
|
87
|
+
c=A.choices(3)
|
54
88
|
assert_equal(3,c.size)
|
55
|
-
assert(
|
56
|
-
assert(
|
57
|
-
assert(
|
89
|
+
assert(A.include?(c[0]))
|
90
|
+
assert(A.include?(c[1]))
|
91
|
+
assert(A.include?(c[2]))
|
58
92
|
}
|
59
93
|
end
|
60
94
|
|
61
95
|
def test_onto
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
assert_equal([[
|
71
|
-
assert_equal([[
|
72
|
-
|
73
|
-
|
96
|
+
assert_equal({'a'=>'w', 'b'=>'x', 'c'=>'y', 'd'=>'z'},A.onto(B))
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_onto_with_empty
|
100
|
+
assert_raise(ArgumentError){ A.onto([]) }
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_slices_with_balanced_results
|
104
|
+
assert_equal([['a','b'],['c','d'],['e','f'],['g','h']], SLICEABLE.slices(2))
|
105
|
+
assert_equal([['a','b','c','d'],['e','f','g','h']], SLICEABLE.slices(4))
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_slices_with_unbalanced_results
|
109
|
+
assert_equal([['a','b','c'],['d','e','f'],['g','h']], SLICEABLE.slices(3))
|
110
|
+
assert_equal([['a','b','c','d','e'],['f','g','h']], SLICEABLE.slices(5))
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_slices_with_empty
|
74
114
|
assert_equal([],[].slices(1))
|
75
|
-
|
76
|
-
|
77
|
-
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_slices_with_negative_count
|
118
|
+
assert_raise(ArgumentError){ [].slices(-1) }
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_slices_with_non_integer_count
|
122
|
+
assert_raise(ArgumentError){ [].slices(0.123) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_slices_with_non_numeric_count
|
126
|
+
assert_raise(ArgumentError){ [].slices("") }
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_divvy_with_one_slice
|
130
|
+
assert_equal([[1,2,3,4,5,6]],[1,2,3,4,5,6].divvy(1))
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_divvy_with_equal_slices
|
134
|
+
assert_equal([[1,2,3],[4,5]],[1,2,3,4,5].divvy(2))
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_divvy_with_remainer_slice
|
138
|
+
assert_equal([[1,2,3],[4,5,6],[7]],[1,2,3,4,5,6,7].divvy(3))
|
78
139
|
end
|
79
140
|
|
80
|
-
def
|
81
|
-
assert_equal([[1,2,3,4,5,6]],[1,2,3,4,5,6].divvy(
|
82
|
-
|
83
|
-
|
84
|
-
|
141
|
+
def test_divvy_with_small_number
|
142
|
+
assert_equal([[1,2],[3,4],[5,6]],[1,2,3,4,5,6].divvy(4))
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_divvy_with_negative_count
|
85
146
|
assert_raise(ArgumentError){ [].divvy(-1) }
|
86
|
-
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_divvy_with_non_integer_count
|
87
150
|
assert_raise(ArgumentError){ [].divvy(0.123) }
|
88
151
|
end
|
89
152
|
|
153
|
+
def test_divvy_with_non_numeric_count
|
154
|
+
assert_raise(ArgumentError){ [].divvy("") }
|
155
|
+
end
|
156
|
+
|
90
157
|
def test_union
|
91
158
|
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
92
159
|
assert_equal([1,2,3,4,5,6],a.union)
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_union_with_empty
|
93
163
|
assert_equal([],[].union)
|
94
164
|
end
|
95
165
|
|
96
166
|
def test_intersect
|
97
167
|
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
98
168
|
assert_equal([3,4],a.intersect)
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_intersect_with_empty
|
99
172
|
assert_equal([],[].intersect)
|
100
173
|
end
|
101
174
|
|
@@ -107,13 +180,21 @@ class ArrayTest < Test::Unit::TestCase
|
|
107
180
|
assert_equal([ 'c'],a.shifted(2))
|
108
181
|
assert_equal([ ],a.shifted(3))
|
109
182
|
assert_equal(nil ,a.shifted(4))
|
110
|
-
assert_raise(ArgumentError){ a.shifted(-1) }
|
111
|
-
assert_raise(ArgumentError){ a.shifted("") }
|
112
|
-
assert_raise(ArgumentError){ a.shifted(0.123) }
|
113
183
|
end
|
114
184
|
|
115
|
-
|
116
|
-
|
185
|
+
def test_shifted_with_negative_count
|
186
|
+
assert_raise(ArgumentError){ [].shifted(-1) }
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_shifted_with_non_integer_count
|
190
|
+
assert_raise(ArgumentError){ [].shifted(0.123) }
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_shifted_with_non_numeric_count
|
194
|
+
assert_raise(ArgumentError){ [].shifted("") }
|
195
|
+
end
|
196
|
+
|
197
|
+
# alias: test_cdr must be idential to test_shifted
|
117
198
|
def test_cdr
|
118
199
|
a=['a','b','c']
|
119
200
|
assert_equal([ 'b','c'],a.cdr)
|
@@ -122,13 +203,24 @@ class ArrayTest < Test::Unit::TestCase
|
|
122
203
|
assert_equal([ 'c'],a.cdr(2))
|
123
204
|
assert_equal([ ],a.cdr(3))
|
124
205
|
assert_equal(nil ,a.cdr(4))
|
125
|
-
assert_raise(ArgumentError){ a.cdr(-1) }
|
126
|
-
assert_raise(ArgumentError){ a.cdr("") }
|
127
|
-
assert_raise(ArgumentError){ a.cdr(0.123) }
|
128
206
|
end
|
129
207
|
|
130
|
-
#
|
131
|
-
|
208
|
+
# alias: test_cdr must be idential to test_shifted
|
209
|
+
def test_cdr_with_negative_count
|
210
|
+
assert_raise(ArgumentError){ [].cdr(-1) }
|
211
|
+
end
|
212
|
+
|
213
|
+
# alias: test_cdr must be idential to test_shifted
|
214
|
+
def test_cdr_with_non_integer_count
|
215
|
+
assert_raise(ArgumentError){ [].cdr(0.123) }
|
216
|
+
end
|
217
|
+
|
218
|
+
# alias: test_cdr must be idential to test_shifted
|
219
|
+
def test_cdr_with_non_numeric_count
|
220
|
+
assert_raise(ArgumentError){ [].cdr("") }
|
221
|
+
end
|
222
|
+
|
223
|
+
# alias: test_rest must be idential to test_shifted
|
132
224
|
def test_rest
|
133
225
|
a=['a','b','c']
|
134
226
|
assert_equal([ 'b','c'],a.rest)
|
@@ -137,9 +229,21 @@ class ArrayTest < Test::Unit::TestCase
|
|
137
229
|
assert_equal([ 'c'],a.rest(2))
|
138
230
|
assert_equal([ ],a.rest(3))
|
139
231
|
assert_equal(nil ,a.rest(4))
|
140
|
-
|
141
|
-
|
142
|
-
|
232
|
+
end
|
233
|
+
|
234
|
+
# alias: test_rest must be idential to test_shifted
|
235
|
+
def test_rest_with_negative_count
|
236
|
+
assert_raise(ArgumentError){ [].rest(-1) }
|
237
|
+
end
|
238
|
+
|
239
|
+
# alias: test_rest must be idential to test_shifted
|
240
|
+
def test_rest_with_non_integer_count
|
241
|
+
assert_raise(ArgumentError){ [].rest(0.123) }
|
242
|
+
end
|
243
|
+
|
244
|
+
# alias: test_rest must be idential to test_shifted
|
245
|
+
def test_rest_with_non_numeric_count
|
246
|
+
assert_raise(ArgumentError){ [].rest("") }
|
143
247
|
end
|
144
248
|
|
145
249
|
def test_shifted_bang
|
@@ -149,10 +253,18 @@ class ArrayTest < Test::Unit::TestCase
|
|
149
253
|
a=['a','b','c']; a.shifted!(2); assert_equal([ 'c'],a)
|
150
254
|
a=['a','b','c']; a.shifted!(3); assert_equal([ ],a)
|
151
255
|
a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
assert_raise(ArgumentError){
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_shifted_bang_with_negative_count
|
259
|
+
assert_raise(ArgumentError){ [].shifted!(-1) }
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_shifted_bang_with_non_integer_count
|
263
|
+
assert_raise(ArgumentError){ [].shifted!(0.123) }
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_shifted_bang_with_non_numeric_count
|
267
|
+
assert_raise(ArgumentError){ [].shifted!("") }
|
156
268
|
end
|
157
269
|
|
158
270
|
# alias: test_cdr_bang must be identical to test_shifted_bang
|
@@ -163,13 +275,24 @@ class ArrayTest < Test::Unit::TestCase
|
|
163
275
|
a=['a','b','c']; a.cdr!(2); assert_equal([ 'c'],a)
|
164
276
|
a=['a','b','c']; a.cdr!(3); assert_equal([ ],a)
|
165
277
|
a=['a','b','c']; a.cdr!(4); assert_equal([ ],a)
|
166
|
-
a=[]
|
167
|
-
assert_raise(ArgumentError){ a.cdr!(-1) }
|
168
|
-
assert_raise(ArgumentError){ a.cdr!("") }
|
169
|
-
assert_raise(ArgumentError){ a.cdr!(0.123) }
|
170
278
|
end
|
171
279
|
|
172
|
-
# alias:
|
280
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
281
|
+
def test_cdr_bang_with_negative_count
|
282
|
+
assert_raise(ArgumentError){ [].cdr!(-1) }
|
283
|
+
end
|
284
|
+
|
285
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
286
|
+
def test_cdr_bang_with_non_integer_count
|
287
|
+
assert_raise(ArgumentError){ [].cdr!(0.123) }
|
288
|
+
end
|
289
|
+
|
290
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
291
|
+
def test_cdr_bang_with_non_numeric_count
|
292
|
+
assert_raise(ArgumentError){ [].cdr!("") }
|
293
|
+
end
|
294
|
+
|
295
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
173
296
|
def test_rest_bang
|
174
297
|
a=['a','b','c']; a.rest!; assert_equal([ 'b','c'],a)
|
175
298
|
a=['a','b','c']; a.rest!(0); assert_equal(['a','b','c'],a)
|
@@ -177,10 +300,21 @@ class ArrayTest < Test::Unit::TestCase
|
|
177
300
|
a=['a','b','c']; a.rest!(2); assert_equal([ 'c'],a)
|
178
301
|
a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
|
179
302
|
a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
303
|
+
end
|
304
|
+
|
305
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
306
|
+
def test_rest_bang_with_negative_count
|
307
|
+
assert_raise(ArgumentError){ [].rest!(-1) }
|
308
|
+
end
|
309
|
+
|
310
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
311
|
+
def test_rest_bang_with_non_integer_count
|
312
|
+
assert_raise(ArgumentError){ [].rest!(0.123) }
|
313
|
+
end
|
314
|
+
|
315
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
316
|
+
def test_rest_bang_with_non_numeric_count
|
317
|
+
assert_raise(ArgumentError){ [].rest!("") }
|
184
318
|
end
|
185
319
|
|
186
320
|
def test_car
|
@@ -208,8 +342,10 @@ class ArrayTest < Test::Unit::TestCase
|
|
208
342
|
end
|
209
343
|
|
210
344
|
def test_to_csv_one_dimensional
|
211
|
-
|
212
|
-
|
345
|
+
assert_equal("a,b,c,d\n",A.to_csv)
|
346
|
+
end
|
347
|
+
|
348
|
+
def test_to_csv_with_empty
|
213
349
|
assert_equal("",[].to_csv)
|
214
350
|
end
|
215
351
|
|
@@ -221,6 +357,9 @@ class ArrayTest < Test::Unit::TestCase
|
|
221
357
|
def test_to_tsv
|
222
358
|
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
223
359
|
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tsv)
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_to_tsv_with_empty
|
224
363
|
assert_equal("",[].to_tsv)
|
225
364
|
end
|
226
365
|
|
@@ -228,6 +367,10 @@ class ArrayTest < Test::Unit::TestCase
|
|
228
367
|
def test_to_tdf
|
229
368
|
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
230
369
|
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tdf)
|
370
|
+
end
|
371
|
+
|
372
|
+
# alias: test_to_tdf must be identical to test_to_tsv
|
373
|
+
def test_to_tdf_with_empty
|
231
374
|
assert_equal("",[].to_tdf)
|
232
375
|
end
|
233
376
|
|