sixarm_ruby_ramp 4.1.0 → 4.2.2
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +30 -6
- data/VERSION +1 -1
- data/lib/sixarm_ruby_ramp/array/join.rb +67 -0
- data/lib/sixarm_ruby_ramp/array/shuffle.rb +49 -0
- data/lib/sixarm_ruby_ramp/array.rb +14 -121
- data/lib/sixarm_ruby_ramp/enumerable/map.rb +114 -0
- data/lib/sixarm_ruby_ramp/enumerable/nitems.rb +66 -0
- data/lib/sixarm_ruby_ramp/enumerable/select.rb +50 -0
- data/lib/sixarm_ruby_ramp/enumerable.rb +1 -124
- data/lib/sixarm_ruby_ramp/integer/rbit.rb +23 -0
- data/lib/sixarm_ruby_ramp.rb +30 -1
- data/test/sixarm_ruby_ramp_test/array/join_test.rb +37 -0
- data/test/sixarm_ruby_ramp_test/array/shuffle_test.rb +26 -0
- data/test/sixarm_ruby_ramp_test/array_test.rb +10 -59
- data/test/sixarm_ruby_ramp_test/enumerable/map_test.rb +42 -0
- data/test/sixarm_ruby_ramp_test/enumerable/nitems_test.rb +48 -0
- data/test/sixarm_ruby_ramp_test/enumerable/select_test.rb +33 -0
- data/test/sixarm_ruby_ramp_test/enumerable_test.rb +0 -136
- data/test/sixarm_ruby_ramp_test/integer/rbit_test.rb +43 -0
- data/test/sixarm_ruby_ramp_test/integer_test.rb +0 -1
- data/test/sixarm_ruby_ramp_test.rb +30 -1
- data.tar.gz.sig +0 -0
- metadata +55 -22
- metadata.gz.sig +0 -0
@@ -1,6 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
# Enumberable extensions
|
2
|
+
# Enumerable extensions
|
4
3
|
|
5
4
|
require 'set'
|
6
5
|
module Enumerable
|
@@ -97,128 +96,6 @@ module Enumerable
|
|
97
96
|
map{|item| yield(item)}.to_h
|
98
97
|
end
|
99
98
|
|
100
|
-
########################################################################
|
101
|
-
#
|
102
|
-
# map_to_xxx
|
103
|
-
#
|
104
|
-
########################################################################
|
105
|
-
|
106
|
-
# Map each item => item.id
|
107
|
-
#
|
108
|
-
# @return [Enumerable<Object>] an list of each item.id
|
109
|
-
#
|
110
|
-
# @example
|
111
|
-
# users = User.find(:all)
|
112
|
-
# users.map_id => [1,2,3,4,...]
|
113
|
-
#
|
114
|
-
# A typical use is to convert a list of ActiveRecord items to a list of id items.
|
115
|
-
#
|
116
|
-
# This method is a fast way to get the same results as items.map(&:id)
|
117
|
-
#
|
118
|
-
def map_id
|
119
|
-
map{|item| item.id}
|
120
|
-
end
|
121
|
-
|
122
|
-
# Map each item => item.to_a
|
123
|
-
#
|
124
|
-
# @return [Enumberable<Array<Object>>] a list of each item.to_a
|
125
|
-
#
|
126
|
-
# @example
|
127
|
-
# set1 = Set.new([:a,:b,:c])
|
128
|
-
# set2 = Set.new([:d,:e,:f])
|
129
|
-
# set3 = Set.new([:g,:h,:i])
|
130
|
-
# sets = [set1, set2, set3]
|
131
|
-
# sets.map_to_a => [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]]
|
132
|
-
#
|
133
|
-
# A typical use is to convert a list with Set items to a list of Array items,
|
134
|
-
# so you can more easily iterate over the the Array items.
|
135
|
-
#
|
136
|
-
# See http://www.ruby-doc.org/core/classes/Enumerable.html#M003148
|
137
|
-
#
|
138
|
-
def map_to_a
|
139
|
-
map{|item| [item]}
|
140
|
-
end
|
141
|
-
|
142
|
-
# Map each item => item.to_f
|
143
|
-
#
|
144
|
-
# @return [Enumerable<Float>] a list of each item.to_f
|
145
|
-
#
|
146
|
-
# @example
|
147
|
-
# strings = ["1","2","3"]
|
148
|
-
# strings.map_to_f => [1.0, 2.0, 3.0]
|
149
|
-
#
|
150
|
-
# A typical use is to convert a list of String items to a list of float items.
|
151
|
-
#
|
152
|
-
# This method is a fast way to get the same results as items.map(&:to_f)
|
153
|
-
#
|
154
|
-
def map_to_f
|
155
|
-
map{|item| item.to_f}
|
156
|
-
end
|
157
|
-
|
158
|
-
# Map each item => item.to_i
|
159
|
-
#
|
160
|
-
# @return [Enumerable<Integer>] a list of each item.to_i
|
161
|
-
#
|
162
|
-
# @example
|
163
|
-
# strings = ["1","2","3"]
|
164
|
-
# strings.map_to_i => [1, 2, 3]
|
165
|
-
#
|
166
|
-
# A typical use is to convert a list of String items to a list of integer items.
|
167
|
-
#
|
168
|
-
# This method is a fast way to get the same results as items.map(&:to_i)
|
169
|
-
#
|
170
|
-
def map_to_i
|
171
|
-
map{|item| item.to_i}
|
172
|
-
end
|
173
|
-
|
174
|
-
# Map each item => item.to_s
|
175
|
-
#
|
176
|
-
# @return [Enumerable<String>] a list of each item.to_s
|
177
|
-
#
|
178
|
-
# @example
|
179
|
-
# numbers = [1, 2, 3]
|
180
|
-
# numbers.map_to_s => ["1", "2", "3"]
|
181
|
-
#
|
182
|
-
# A typical use is to convert a list of Numeric items to a list of String items.
|
183
|
-
#
|
184
|
-
# This method is a fast way to get the same results as items.map(&:to_s)
|
185
|
-
#
|
186
|
-
def map_to_s
|
187
|
-
map{|item| item.to_s}
|
188
|
-
end
|
189
|
-
|
190
|
-
# Map each item => item.to_sym
|
191
|
-
#
|
192
|
-
# @return [Enumerable<Symbol>] a list of each item.to_sym
|
193
|
-
#
|
194
|
-
# @example
|
195
|
-
# strings = ["foo", "goo", "hoo"]
|
196
|
-
# strings.map_to_sym => [:foo, :goo, :hoo]
|
197
|
-
#
|
198
|
-
# A typical use is to convert a list of Object items to a list of Symbol items.
|
199
|
-
#
|
200
|
-
# This method is a fast way to get the same results as items.map(&:to_sym)
|
201
|
-
#
|
202
|
-
def map_to_sym
|
203
|
-
map{|item| item.to_sym}
|
204
|
-
end
|
205
|
-
|
206
|
-
# Map each item and its index => a new output
|
207
|
-
#
|
208
|
-
# @return [Enumerable<Object>] an list of each item transformed by the block
|
209
|
-
# @see Enumerable#map
|
210
|
-
# @see Enumerable#each_with_index
|
211
|
-
#
|
212
|
-
# @example
|
213
|
-
# strings = ["a", "b", "c"]
|
214
|
-
# strings.map_with_index{|string,index| "#{string}#{index}"}
|
215
|
-
# => ["a0, "b1", "c3"]
|
216
|
-
#
|
217
|
-
def map_with_index
|
218
|
-
index=-1
|
219
|
-
map{|item| index+=1; yield(item,index)}
|
220
|
-
end
|
221
|
-
|
222
99
|
########################################################################
|
223
100
|
#
|
224
101
|
# select
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Integer
|
2
|
+
|
3
|
+
# Reverse bit.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# 0.rbit #=> 0
|
8
|
+
# 1.rbit #=> 128
|
9
|
+
# 2.rbit #=> 64
|
10
|
+
#
|
11
|
+
# The bit count defaults to 8.
|
12
|
+
#
|
13
|
+
# @param count [Integer] the count of bits to reverse
|
14
|
+
#
|
15
|
+
def rbit(count=8)
|
16
|
+
z = self & 0
|
17
|
+
count.times{|i|
|
18
|
+
z = z * 2 + self[i]
|
19
|
+
}
|
20
|
+
z
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/lib/sixarm_ruby_ramp.rb
CHANGED
@@ -3,6 +3,35 @@
|
|
3
3
|
Please see README
|
4
4
|
=end
|
5
5
|
|
6
|
-
[
|
6
|
+
[
|
7
|
+
'array',
|
8
|
+
'array/join',
|
9
|
+
'array/shuffle',
|
10
|
+
'class',
|
11
|
+
'csv',
|
12
|
+
'date',
|
13
|
+
'enumerable',
|
14
|
+
'enumerable/map',
|
15
|
+
'enumerable/nitems',
|
16
|
+
'enumerable/select',
|
17
|
+
'file',
|
18
|
+
'fixnum',
|
19
|
+
'hash',
|
20
|
+
'integer',
|
21
|
+
'integer/rbit',
|
22
|
+
'io',
|
23
|
+
'kernel',
|
24
|
+
'math',
|
25
|
+
'nil',
|
26
|
+
'numeric',
|
27
|
+
'object',
|
28
|
+
'pairable',
|
29
|
+
'process',
|
30
|
+
'string',
|
31
|
+
'symbol',
|
32
|
+
'time',
|
33
|
+
'xml',
|
34
|
+
'yaml'
|
35
|
+
].map{|x|
|
7
36
|
require File.dirname(__FILE__) + "/sixarm_ruby_ramp/#{x}.rb"
|
8
37
|
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'sixarm_ruby_ramp/array/join'
|
4
|
+
|
5
|
+
class ArrayJoinTest < Minitest::Test
|
6
|
+
|
7
|
+
A=['a','b','c']
|
8
|
+
|
9
|
+
def test_join_with_no_ops
|
10
|
+
assert_equal('abc',A.join())
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_join_with_1_op_does_infix
|
14
|
+
assert_equal('a*b*c', A.join('*'))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_join_with_2_ops_does_prefix_suffix
|
18
|
+
assert_equal('[a][b][c]', A.join('[',']'))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_join_with_3_ops_does_prefix_suffix_infix
|
22
|
+
assert_equal('[a]*[b]*[c]', A.join('[',']','*'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_join_with_too_many_ops_raises
|
26
|
+
assert_raises(ArgumentError){ A.join('','','','') }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_join_prefix_suffix
|
30
|
+
assert_equal('[a][b][c]', A.join_prefix_suffix('[',']'))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_join_prefix_suffix_infix
|
34
|
+
assert_equal('[a]*[b]*[c]', A.join_prefix_suffix_infix('[',']','*'))
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'sixarm_ruby_ramp/array/shuffle'
|
4
|
+
|
5
|
+
class ArrayTest < Minitest::Test
|
6
|
+
|
7
|
+
def test_shuffle
|
8
|
+
a=[1,2,3,4]
|
9
|
+
5.times {
|
10
|
+
b=a.shuffle
|
11
|
+
assert_equal(a.size,b.size)
|
12
|
+
a.each{|item| assert(b.include?(item)) }
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_shuffle_bang
|
17
|
+
a=[1,2,3,4]
|
18
|
+
b=a.dup
|
19
|
+
5.times {
|
20
|
+
b.shuffle!
|
21
|
+
assert_equal(a.size,b.size)
|
22
|
+
a.each{|item| assert(b.include?(item)) }
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -9,34 +9,6 @@ class ArrayTest < Minitest::Test
|
|
9
9
|
A=['a','b','c','d']
|
10
10
|
B=['w','x','y','z']
|
11
11
|
|
12
|
-
def test_join_with_no_ops
|
13
|
-
assert_equal('abcd',A.join())
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_join_with_1_op_does_infix
|
17
|
-
assert_equal('a*b*c*d', A.join('*'))
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_join_with_2_ops_does_prefix_suffix
|
21
|
-
assert_equal('[a][b][c][d]', A.join('[',']'))
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_join_with_3_opes_does_prefix_suffix_infix
|
25
|
-
assert_equal('[a]*[b]*[c]*[d]', A.join('[',']','*'))
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_join_with_too_many_ops_raises
|
29
|
-
assert_raises(ArgumentError){ A.join('','','','') }
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_join_prefix_suffix
|
33
|
-
assert_equal('[a][b][c][d]', A.join_prefix_suffix('[',']'))
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_join_prefix_suffix_infix
|
37
|
-
assert_equal('[a]*[b]*[c]*[d]', A.join_prefix_suffix_infix('[',']','*'))
|
38
|
-
end
|
39
|
-
|
40
12
|
def test_size_with_one_item
|
41
13
|
assert_equal(true,[1].size?)
|
42
14
|
end
|
@@ -56,7 +28,7 @@ class ArrayTest < Minitest::Test
|
|
56
28
|
def test_size_with_one_empty
|
57
29
|
assert_equal(true,[[]].size?)
|
58
30
|
end
|
59
|
-
|
31
|
+
|
60
32
|
def test_rotate
|
61
33
|
a=A.dup
|
62
34
|
a.rotate!
|
@@ -157,7 +129,7 @@ class ArrayTest < Minitest::Test
|
|
157
129
|
end
|
158
130
|
|
159
131
|
# alias: test_cdr must be idential to test_shifted
|
160
|
-
def test_cdr_with_negative_count
|
132
|
+
def test_cdr_with_negative_count
|
161
133
|
assert_raises(ArgumentError){ [].cdr(-1) }
|
162
134
|
end
|
163
135
|
|
@@ -206,15 +178,15 @@ class ArrayTest < Minitest::Test
|
|
206
178
|
a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
|
207
179
|
end
|
208
180
|
|
209
|
-
def test_shifted_bang_with_negative_count
|
181
|
+
def test_shifted_bang_with_negative_count
|
210
182
|
assert_raises(ArgumentError){ [].shifted!(-1) }
|
211
183
|
end
|
212
184
|
|
213
|
-
def test_shifted_bang_with_non_integer_count
|
185
|
+
def test_shifted_bang_with_non_integer_count
|
214
186
|
assert_raises(ArgumentError){ [].shifted!(0.123) }
|
215
187
|
end
|
216
188
|
|
217
|
-
def test_shifted_bang_with_non_numeric_count
|
189
|
+
def test_shifted_bang_with_non_numeric_count
|
218
190
|
assert_raises(ArgumentError){ [].shifted!("") }
|
219
191
|
end
|
220
192
|
|
@@ -242,7 +214,7 @@ class ArrayTest < Minitest::Test
|
|
242
214
|
def test_cdr_bang_with_non_numeric_count
|
243
215
|
assert_raises(ArgumentError){ [].cdr!("") }
|
244
216
|
end
|
245
|
-
|
217
|
+
|
246
218
|
# alias: test_rest_bang must be identical to test_shifted_bang
|
247
219
|
def test_rest_bang
|
248
220
|
a=['a','b','c']; a.rest!; assert_equal([ 'b','c'],a)
|
@@ -252,7 +224,7 @@ class ArrayTest < Minitest::Test
|
|
252
224
|
a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
|
253
225
|
a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
|
254
226
|
end
|
255
|
-
|
227
|
+
|
256
228
|
# alias: test_rest_bang must be identical to test_shifted_bang
|
257
229
|
def test_rest_bang_with_negative_count
|
258
230
|
assert_raises(ArgumentError){ [].rest!(-1) }
|
@@ -273,31 +245,12 @@ class ArrayTest < Minitest::Test
|
|
273
245
|
assert_equal('a',a.car)
|
274
246
|
end
|
275
247
|
|
276
|
-
def test_shuffle
|
277
|
-
a=[1,2,3,4]
|
278
|
-
5.times {
|
279
|
-
b=a.shuffle
|
280
|
-
assert_equal(a.size,b.size)
|
281
|
-
a.each{|item| assert(b.include?(item)) }
|
282
|
-
}
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_shuffle_bang
|
286
|
-
a=[1,2,3,4]
|
287
|
-
b=a.dup
|
288
|
-
5.times {
|
289
|
-
b.shuffle!
|
290
|
-
assert_equal(a.size,b.size)
|
291
|
-
a.each{|item| assert(b.include?(item)) }
|
292
|
-
}
|
293
|
-
end
|
294
|
-
|
295
248
|
def test_to_csv_one_dimensional
|
296
249
|
assert_equal("a,b,c,d\n",A.to_csv)
|
297
250
|
end
|
298
251
|
|
299
252
|
def test_to_csv_with_empty
|
300
|
-
assert_equal("",[].to_csv)
|
253
|
+
assert_equal("",[].to_csv)
|
301
254
|
end
|
302
255
|
|
303
256
|
def test_to_csv_multi_dimensional
|
@@ -311,7 +264,7 @@ class ArrayTest < Minitest::Test
|
|
311
264
|
end
|
312
265
|
|
313
266
|
def test_to_tsv_with_empty
|
314
|
-
assert_equal("",[].to_tsv)
|
267
|
+
assert_equal("",[].to_tsv)
|
315
268
|
end
|
316
269
|
|
317
270
|
# alias: test_to_tdf must be identical to test_to_tsv
|
@@ -322,9 +275,7 @@ class ArrayTest < Minitest::Test
|
|
322
275
|
|
323
276
|
# alias: test_to_tdf must be identical to test_to_tsv
|
324
277
|
def test_to_tdf_with_empty
|
325
|
-
assert_equal("",[].to_tdf)
|
278
|
+
assert_equal("",[].to_tdf)
|
326
279
|
end
|
327
280
|
|
328
281
|
end
|
329
|
-
|
330
|
-
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'sixarm_ruby_ramp/enumerable/map'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
class EnumerableMapTest < Minitest::Test
|
7
|
+
|
8
|
+
def test_map_id
|
9
|
+
x = [OpenStruct.new(id: 1), OpenStruct.new(id: 2), OpenStruct.new(id: 3)]
|
10
|
+
assert_equal(x.map(&:id), x.map_id)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_map_to_a
|
14
|
+
x = [{a: :b, c: :d}, {e: :f, g: :h}]
|
15
|
+
assert_equal(x.map(&:to_a), x.map_to_a)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_map_to_f
|
19
|
+
x = ["1", "2", "3"]
|
20
|
+
assert_equal(x.map(&:to_f) , x.map_to_f)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_map_to_i
|
24
|
+
x = ["1", "2", "3"]
|
25
|
+
assert_equal(x.map(&:to_i), x.map_to_i)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_map_to_s
|
29
|
+
x = [1, 2, 3]
|
30
|
+
assert_equal(x.map(&:to_s), x.map_to_s)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_map_to_sym
|
34
|
+
x = ["a", "b", "c"]
|
35
|
+
assert_equal(x.map(&:to_sym) , x.map_to_sym)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_map_with_index
|
39
|
+
assert_equal(["a0", "b1", "c2"], ["a", "b", "c"].map_with_index{|x,i| "#{x}#{i}"})
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'sixarm_ruby_ramp/enumerable/nitems'
|
4
|
+
|
5
|
+
class EnumerableNItemsTest < Minitest::Test
|
6
|
+
|
7
|
+
ITEMS = ['a','b','c']
|
8
|
+
|
9
|
+
def test_nitems?
|
10
|
+
assert_equal(true, ITEMS.nitems?(0){|x| x < 'a'})
|
11
|
+
assert_equal(false, ITEMS.nitems?(1){|x| x < 'a'})
|
12
|
+
assert_equal(false, ITEMS.nitems?(2){|x| x < 'a'})
|
13
|
+
assert_equal(false, ITEMS.nitems?(0){|x| x < 'b'})
|
14
|
+
assert_equal(true, ITEMS.nitems?(1){|x| x < 'b'})
|
15
|
+
assert_equal(false, ITEMS.nitems?(2){|x| x < 'b'})
|
16
|
+
assert_equal(false, ITEMS.nitems?(0){|x| x < 'c'})
|
17
|
+
assert_equal(false, ITEMS.nitems?(1){|x| x < 'c'})
|
18
|
+
assert_equal(true, ITEMS.nitems?(2){|x| x < 'c'})
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def test_nitems_while
|
23
|
+
assert_equal(0,ITEMS.nitems_while{|x| x < 'a' },'< a')
|
24
|
+
assert_equal(1,ITEMS.nitems_while{|x| x < 'b' },'< b')
|
25
|
+
assert_equal(2,ITEMS.nitems_while{|x| x < 'c' },'< c')
|
26
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'd' },'< d')
|
27
|
+
assert_equal(3,ITEMS.nitems_while{|x| x < 'e' },'< e')
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_nitems_until
|
32
|
+
assert_equal(0,ITEMS.nitems_until{|x| x == 'a' },'a')
|
33
|
+
assert_equal(1,ITEMS.nitems_until{|x| x == 'b' },'b')
|
34
|
+
assert_equal(2,ITEMS.nitems_until{|x| x == 'c' },'c')
|
35
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'd' },'d')
|
36
|
+
assert_equal(3,ITEMS.nitems_until{|x| x == 'e' },'e')
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def test_nitems_with_index
|
41
|
+
assert_equal(0,ITEMS.nitems_with_index{|x,i| i < 0 },'i < 0')
|
42
|
+
assert_equal(1,ITEMS.nitems_with_index{|x,i| i < 1 },'i < 1')
|
43
|
+
assert_equal(2,ITEMS.nitems_with_index{|x,i| i < 2 },'i < 2')
|
44
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 3 },'i < 3')
|
45
|
+
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 4 },'i < 4')
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'sixarm_ruby_ramp/enumerable/select'
|
4
|
+
|
5
|
+
class EnumerableSelectTest < Minitest::Test
|
6
|
+
|
7
|
+
ITEMS = ['a','b','c']
|
8
|
+
|
9
|
+
def test_select_while
|
10
|
+
assert_equal([ ], ITEMS.select_while{|x| x < 'a' },'< a')
|
11
|
+
assert_equal(['a' ], ITEMS.select_while{|x| x < 'b' },'< b')
|
12
|
+
assert_equal(['a','b' ], ITEMS.select_while{|x| x < 'c' },'< c')
|
13
|
+
assert_equal(['a','b','c'], ITEMS.select_while{|x| x < 'd' },'< d')
|
14
|
+
assert_equal(['a','b','c'], ITEMS.select_while{|x| x < 'e' },'< e')
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_select_until_condition
|
18
|
+
assert_equal([ ], ITEMS.select_until{|x| x == 'a' },'a')
|
19
|
+
assert_equal(['a' ], ITEMS.select_until{|x| x == 'b' },'b')
|
20
|
+
assert_equal(['a','b' ], ITEMS.select_until{|x| x == 'c' },'c')
|
21
|
+
assert_equal(['a','b','c'], ITEMS.select_until{|x| x == 'd' },'d')
|
22
|
+
assert_equal(['a','b','c'], ITEMS.select_until{|x| x == 'e' },'e')
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_select_with_index
|
26
|
+
assert_equal([ ], ITEMS.select_with_index{|x,i| i < 0 },'i < 0')
|
27
|
+
assert_equal(['a' ], ITEMS.select_with_index{|x,i| i < 1 },'i < 1')
|
28
|
+
assert_equal(['a','b' ], ITEMS.select_with_index{|x,i| i < 2 },'i < 2')
|
29
|
+
assert_equal(['a','b','c'], ITEMS.select_with_index{|x,i| i < 3 },'i < 3')
|
30
|
+
assert_equal(['a','b','c'], ITEMS.select_with_index{|x,i| i < 4 },'i < 4')
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -7,7 +7,6 @@ require 'set'
|
|
7
7
|
class EnumerableTest < Minitest::Test
|
8
8
|
|
9
9
|
ITEMS = ['a','b','c']
|
10
|
-
MAPTEST = [123,"456"] # to test typecasts-- one is numeric and one is string
|
11
10
|
RGB = ["red","green","blue"] # to test case changes
|
12
11
|
|
13
12
|
|
@@ -47,95 +46,6 @@ class EnumerableTest < Minitest::Test
|
|
47
46
|
end
|
48
47
|
|
49
48
|
|
50
|
-
########################################################################
|
51
|
-
#
|
52
|
-
# map
|
53
|
-
#
|
54
|
-
########################################################################
|
55
|
-
|
56
|
-
|
57
|
-
class Mock #:nodoc: all
|
58
|
-
attr_accessor :id
|
59
|
-
|
60
|
-
def initialize(id)
|
61
|
-
@id=id
|
62
|
-
end
|
63
|
-
|
64
|
-
def to_a
|
65
|
-
[self]
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
|
71
|
-
A = Mock.new('a')
|
72
|
-
B = Mock.new('b')
|
73
|
-
C = Mock.new('c')
|
74
|
-
|
75
|
-
|
76
|
-
def test_map_id
|
77
|
-
assert_equal(['a','b','c'],[A,B,C].map_id)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_map_to_a
|
81
|
-
assert_equal([[123],["456"]],MAPTEST.to_set.map_to_a)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_map_to_f
|
85
|
-
assert_equal([123.0,456.0],MAPTEST.map_to_f)
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_map_to_i
|
89
|
-
assert_equal([123,456],MAPTEST.map_to_i)
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_map_to_s
|
93
|
-
assert_equal(["123","456"],MAPTEST.map_to_s)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_map_to_sym
|
97
|
-
assert_equal([:a,:b,:c],ITEMS.map_to_sym)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_map_with_index
|
101
|
-
assert_equal(['a0','b1','c2'],ITEMS.map_with_index{|x,i| "#{x}#{i}"})
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
########################################################################
|
106
|
-
#
|
107
|
-
# select
|
108
|
-
#
|
109
|
-
########################################################################
|
110
|
-
|
111
|
-
|
112
|
-
def test_select_while
|
113
|
-
assert_equal([ ],ITEMS.select_while{|x| x < 'a' },'< a')
|
114
|
-
assert_equal(['a' ],ITEMS.select_while{|x| x < 'b' },'< b')
|
115
|
-
assert_equal(['a','b' ],ITEMS.select_while{|x| x < 'c' },'< c')
|
116
|
-
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'd' },'< d')
|
117
|
-
assert_equal(['a','b','c'],ITEMS.select_while{|x| x < 'e' },'< e')
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
def test_select_until_condition
|
122
|
-
assert_equal([ ],ITEMS.select_until{|x| x == 'a' },'a')
|
123
|
-
assert_equal(['a' ],ITEMS.select_until{|x| x == 'b' },'b')
|
124
|
-
assert_equal(['a','b' ],ITEMS.select_until{|x| x == 'c' },'c')
|
125
|
-
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'd' },'d')
|
126
|
-
assert_equal(['a','b','c'],ITEMS.select_until{|x| x == 'e' },'e')
|
127
|
-
end
|
128
|
-
|
129
|
-
|
130
|
-
def test_select_with_index
|
131
|
-
assert_equal([ ],ITEMS.select_with_index{|x,i| i < 0 },'i < 0')
|
132
|
-
assert_equal(['a' ],ITEMS.select_with_index{|x,i| i < 1 },'i < 1')
|
133
|
-
assert_equal(['a','b' ],ITEMS.select_with_index{|x,i| i < 2 },'i < 2')
|
134
|
-
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 3 },'i < 3')
|
135
|
-
assert_equal(['a','b','c'],ITEMS.select_with_index{|x,i| i < 4 },'i < 4')
|
136
|
-
end
|
137
|
-
|
138
|
-
|
139
49
|
########################################################################
|
140
50
|
#
|
141
51
|
# bisect
|
@@ -164,52 +74,6 @@ class EnumerableTest < Minitest::Test
|
|
164
74
|
end
|
165
75
|
|
166
76
|
|
167
|
-
########################################################################
|
168
|
-
#
|
169
|
-
# nitems
|
170
|
-
#
|
171
|
-
########################################################################
|
172
|
-
|
173
|
-
def test_nitems?
|
174
|
-
assert_equal(true, ITEMS.nitems?(0){|x| x < 'a'})
|
175
|
-
assert_equal(false, ITEMS.nitems?(1){|x| x < 'a'})
|
176
|
-
assert_equal(false, ITEMS.nitems?(2){|x| x < 'a'})
|
177
|
-
assert_equal(false, ITEMS.nitems?(0){|x| x < 'b'})
|
178
|
-
assert_equal(true, ITEMS.nitems?(1){|x| x < 'b'})
|
179
|
-
assert_equal(false, ITEMS.nitems?(2){|x| x < 'b'})
|
180
|
-
assert_equal(false, ITEMS.nitems?(0){|x| x < 'c'})
|
181
|
-
assert_equal(false, ITEMS.nitems?(1){|x| x < 'c'})
|
182
|
-
assert_equal(true, ITEMS.nitems?(2){|x| x < 'c'})
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
def test_nitems_while
|
187
|
-
assert_equal(0,ITEMS.nitems_while{|x| x < 'a' },'< a')
|
188
|
-
assert_equal(1,ITEMS.nitems_while{|x| x < 'b' },'< b')
|
189
|
-
assert_equal(2,ITEMS.nitems_while{|x| x < 'c' },'< c')
|
190
|
-
assert_equal(3,ITEMS.nitems_while{|x| x < 'd' },'< d')
|
191
|
-
assert_equal(3,ITEMS.nitems_while{|x| x < 'e' },'< e')
|
192
|
-
end
|
193
|
-
|
194
|
-
|
195
|
-
def test_nitems_until
|
196
|
-
assert_equal(0,ITEMS.nitems_until{|x| x == 'a' },'a')
|
197
|
-
assert_equal(1,ITEMS.nitems_until{|x| x == 'b' },'b')
|
198
|
-
assert_equal(2,ITEMS.nitems_until{|x| x == 'c' },'c')
|
199
|
-
assert_equal(3,ITEMS.nitems_until{|x| x == 'd' },'d')
|
200
|
-
assert_equal(3,ITEMS.nitems_until{|x| x == 'e' },'e')
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
def test_nitems_with_index
|
205
|
-
assert_equal(0,ITEMS.nitems_with_index{|x,i| i < 0 },'i < 0')
|
206
|
-
assert_equal(1,ITEMS.nitems_with_index{|x,i| i < 1 },'i < 1')
|
207
|
-
assert_equal(2,ITEMS.nitems_with_index{|x,i| i < 2 },'i < 2')
|
208
|
-
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 3 },'i < 3')
|
209
|
-
assert_equal(3,ITEMS.nitems_with_index{|x,i| i < 4 },'i < 4')
|
210
|
-
end
|
211
|
-
|
212
|
-
|
213
77
|
########################################################################
|
214
78
|
#
|
215
79
|
# strings
|