tbpgr_utils 0.0.23 → 0.0.24
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +41 -2
- data/lib/open_classes/array.rb +349 -312
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array_spec.rb +752 -678
- metadata +12 -12
data/lib/open_classes/array.rb
CHANGED
@@ -1,312 +1,349 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'open_classes/object'
|
3
|
-
require 'open_classes/module'
|
4
|
-
|
5
|
-
# Array
|
6
|
-
class Array
|
7
|
-
# Arrays loop together.
|
8
|
-
#
|
9
|
-
# alpha = %w{one two three}
|
10
|
-
# numbers = %w{1 2 3}
|
11
|
-
# [alpha, numbers].together do |first, second|
|
12
|
-
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
13
|
-
# end
|
14
|
-
def together
|
15
|
-
if_not_contain_array_rails_type_error
|
16
|
-
first.each_with_index do |i_v, i|
|
17
|
-
eval_each_str = get_args_str_for_together i
|
18
|
-
instance_eval "yield(#{eval_each_str})"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Arrays loop together with index.
|
23
|
-
#
|
24
|
-
# alpha = %w{one two three}
|
25
|
-
# numbers = %w{1 2 3}
|
26
|
-
# [alpha, numbers].together_with_index do |first, second, index|
|
27
|
-
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
28
|
-
# end
|
29
|
-
def together_with_index
|
30
|
-
if_not_contain_array_rails_type_error
|
31
|
-
first.each_with_index do |i_v, i|
|
32
|
-
eval_each_str = get_args_str_for_together i, true
|
33
|
-
instance_eval "yield(#{eval_each_str})"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Arrays together map.
|
38
|
-
#
|
39
|
-
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
40
|
-
#
|
41
|
-
# if you want to single Array return
|
42
|
-
# alpha = %w{one two three}
|
43
|
-
# numbers = %w{1 2 3}
|
44
|
-
# print [alpha, numbers].together_map do |first, second|
|
45
|
-
# "#{first}:#{second}\n"
|
46
|
-
# end # => output one:1, two:2, three:3
|
47
|
-
#
|
48
|
-
# if you want to multi Array return
|
49
|
-
# alpha = %w{one two three}
|
50
|
-
# numbers = %w{1 2 3}
|
51
|
-
# print [alpha, numbers].together_map do |first, second|
|
52
|
-
# ["#{first}:#{second}", "#{second}:#{first}"]
|
53
|
-
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
54
|
-
def together_map
|
55
|
-
if_not_contain_array_rails_type_error
|
56
|
-
ret = []
|
57
|
-
first.each_with_index do |i_v, i|
|
58
|
-
eval_each_str = get_args_str_for_together i
|
59
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
60
|
-
ret = set_together_each_return_map(ret, each_ret, i)
|
61
|
-
end
|
62
|
-
ret
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
# Arrays together map!.
|
67
|
-
#
|
68
|
-
# together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
|
69
|
-
#
|
70
|
-
# if you want to single Array return
|
71
|
-
# alpha = %w{one two three}
|
72
|
-
# numbers = %w{1 2 3}
|
73
|
-
# ary = [alpha, numbers]
|
74
|
-
# ret = ary.together_map! do |first, second|
|
75
|
-
# "#{first}:#{second}"
|
76
|
-
# end
|
77
|
-
# print ret # => output ['one:1', 'two:2', 'three:3']
|
78
|
-
# print ary # => output ['one:1', 'two:2', 'three:3']
|
79
|
-
#
|
80
|
-
# if you want to multi Array return
|
81
|
-
# alpha = %w{one two three}
|
82
|
-
# numbers = %w{1 2 3}
|
83
|
-
# ary = [alpha, numbers]
|
84
|
-
# ret = ary.together_map! do |first, second|
|
85
|
-
# ["#{first}:#{second}", "#{second}:#{first}"]
|
86
|
-
# end
|
87
|
-
# print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
88
|
-
# print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
89
|
-
def together_map!
|
90
|
-
if_not_contain_array_rails_type_error
|
91
|
-
ret = []
|
92
|
-
first.each_with_index do |i_v, i|
|
93
|
-
eval_each_str = get_args_str_for_together i
|
94
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
95
|
-
ret = set_together_each_return_map(ret, each_ret, i)
|
96
|
-
end
|
97
|
-
clear
|
98
|
-
ret.each { |v|self << v }
|
99
|
-
end
|
100
|
-
|
101
|
-
# Arrays loop together select.
|
102
|
-
#
|
103
|
-
# together_select has aliases [:tselect, :together_find_all, :tfindall]
|
104
|
-
#
|
105
|
-
# if you want to single Array return
|
106
|
-
# firsts = [1, 2, 3, 4]
|
107
|
-
# seconds = [4, 2, 3, 1]
|
108
|
-
# ret = [firsts, seconds].together_select{|first, second|first == second}
|
109
|
-
# print ret # => output [[2, 3], [2, 3]]
|
110
|
-
#
|
111
|
-
# if you want to multi Array return
|
112
|
-
# firsts = [1, 2, 3, 4]
|
113
|
-
# seconds = [4, 2, 3, 1]
|
114
|
-
# ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
|
115
|
-
# print ret # => output [[1, 3], [4, 2]]
|
116
|
-
def together_select
|
117
|
-
if_not_contain_array_rails_type_error
|
118
|
-
ret = []
|
119
|
-
first.each_with_index do |i_v, i|
|
120
|
-
eval_each_str = get_args_str_for_together i
|
121
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
122
|
-
ret = set_together_each_return_select(ret, each_ret, i)
|
123
|
-
end
|
124
|
-
ret
|
125
|
-
end
|
126
|
-
|
127
|
-
# Arrays loop together reduce.
|
128
|
-
#
|
129
|
-
# together_reduce has aliases [:treduce, :together_inject, :tinject]
|
130
|
-
#
|
131
|
-
# if you want to single return
|
132
|
-
# firsts = [1, 2, 3, 4]
|
133
|
-
# seconds = [4, 2, 3, 1]
|
134
|
-
# ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
|
135
|
-
# print ret # => output 20
|
136
|
-
#
|
137
|
-
# if you want to single return with init value
|
138
|
-
# firsts = [1, 2, 3, 4]
|
139
|
-
# seconds = [4, 2, 3, 1]
|
140
|
-
# ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
|
141
|
-
# print ret # => output 30
|
142
|
-
#
|
143
|
-
# if you want to single return with init string value
|
144
|
-
# firsts = %w{a b c}
|
145
|
-
# seconds = %w{1 2 3}
|
146
|
-
# ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
|
147
|
-
# print ret # => output 'start-a1b2c3'
|
148
|
-
#
|
149
|
-
# if you want to single return with init Array value
|
150
|
-
# firsts = [1, 2, 3, 4]
|
151
|
-
# seconds = [4, 2, 3, 1]
|
152
|
-
# ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
|
153
|
-
# print ret # => output [5, 4, 6, 5]
|
154
|
-
#
|
155
|
-
# if you want to single return with init Hash value
|
156
|
-
# firsts = [1, 2, 3, 4]
|
157
|
-
# seconds = [4, 2, 3, 1]
|
158
|
-
# ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
|
159
|
-
# print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
|
160
|
-
def together_reduce(init = nil)
|
161
|
-
if_not_contain_array_rails_type_error
|
162
|
-
memo = initial_memo init
|
163
|
-
first.each_with_index do |i_v, i|
|
164
|
-
eval_each_str = get_args_str_for_together i
|
165
|
-
memo = instance_eval "yield(memo, #{eval_each_str})"
|
166
|
-
end
|
167
|
-
memo
|
168
|
-
end
|
169
|
-
|
170
|
-
# Arrays bulk concat.
|
171
|
-
#
|
172
|
-
# together_concat has alias :tconcat
|
173
|
-
#
|
174
|
-
# alpha = %w{one two three}
|
175
|
-
# numbers = %w{1 2 3}
|
176
|
-
# [alpha, numbers].together do |first, second|
|
177
|
-
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
178
|
-
# end
|
179
|
-
def together_concat(other)
|
180
|
-
if_not_contain_array_rails_type_error
|
181
|
-
each { |list|list.concat other }
|
182
|
-
end
|
183
|
-
|
184
|
-
# Arrays bulk at.
|
185
|
-
#
|
186
|
-
# together_at has alias :tat
|
187
|
-
#
|
188
|
-
# same elements size case
|
189
|
-
# alpha = %w{one two three}
|
190
|
-
# numbers = %w{1 2 3}
|
191
|
-
# [alpha, numbers].together_at 2 # => output ['three', 3]
|
192
|
-
#
|
193
|
-
# different elements size case
|
194
|
-
# alpha = %w{one two three}
|
195
|
-
# numbers = %w{1 2}
|
196
|
-
# [alpha, numbers].together_at 2 # => output ['three', nil]
|
197
|
-
def together_at(index)
|
198
|
-
if_not_contain_array_rails_type_error
|
199
|
-
reduce([]) { |ats, list|ats << list.at(index) }
|
200
|
-
end
|
201
|
-
|
202
|
-
# Arrays bulk clear.
|
203
|
-
#
|
204
|
-
# together_clear has alias :tclear
|
205
|
-
#
|
206
|
-
# same elements size case
|
207
|
-
# alpha = %w{one two three}
|
208
|
-
# numbers = %w{1 2 3}
|
209
|
-
# [alpha, numbers].together_clear # => output [[],[]]
|
210
|
-
def together_clear
|
211
|
-
if_not_contain_array_rails_type_error
|
212
|
-
each { |list|list.clear }
|
213
|
-
end
|
214
|
-
|
215
|
-
# Arrays bulk compact.(immutable)
|
216
|
-
#
|
217
|
-
# together_compact has alias :tcompact
|
218
|
-
#
|
219
|
-
# same elements size case
|
220
|
-
# alpha = ['a','b','c', nil,'d']
|
221
|
-
# numbers = [1, 2, nil, 3]
|
222
|
-
# lists = [alpha, numbers]
|
223
|
-
# ret = lists.together_compact
|
224
|
-
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
225
|
-
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
226
|
-
def together_compact
|
227
|
-
if_not_contain_array_rails_type_error
|
228
|
-
reduce([]) { |ret, list|ret << list.compact }
|
229
|
-
end
|
230
|
-
|
231
|
-
# Arrays bulk compact!.(mutable)
|
232
|
-
#
|
233
|
-
# together_compact! has alias :tcompact!
|
234
|
-
#
|
235
|
-
# same elements size case
|
236
|
-
# alpha = ['a','b','c', nil,'d']
|
237
|
-
# numbers = [1, 2, nil, 3]
|
238
|
-
#
|
239
|
-
#
|
240
|
-
# print
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
#
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
end
|
293
|
-
|
294
|
-
def
|
295
|
-
|
296
|
-
|
297
|
-
end
|
298
|
-
|
299
|
-
def
|
300
|
-
(
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
|
5
|
+
# Array
|
6
|
+
class Array
|
7
|
+
# Arrays loop together.
|
8
|
+
#
|
9
|
+
# alpha = %w{one two three}
|
10
|
+
# numbers = %w{1 2 3}
|
11
|
+
# [alpha, numbers].together do |first, second|
|
12
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
13
|
+
# end
|
14
|
+
def together
|
15
|
+
if_not_contain_array_rails_type_error
|
16
|
+
first.each_with_index do |i_v, i|
|
17
|
+
eval_each_str = get_args_str_for_together i
|
18
|
+
instance_eval "yield(#{eval_each_str})"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Arrays loop together with index.
|
23
|
+
#
|
24
|
+
# alpha = %w{one two three}
|
25
|
+
# numbers = %w{1 2 3}
|
26
|
+
# [alpha, numbers].together_with_index do |first, second, index|
|
27
|
+
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
28
|
+
# end
|
29
|
+
def together_with_index
|
30
|
+
if_not_contain_array_rails_type_error
|
31
|
+
first.each_with_index do |i_v, i|
|
32
|
+
eval_each_str = get_args_str_for_together i, true
|
33
|
+
instance_eval "yield(#{eval_each_str})"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Arrays together map.
|
38
|
+
#
|
39
|
+
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
40
|
+
#
|
41
|
+
# if you want to single Array return
|
42
|
+
# alpha = %w{one two three}
|
43
|
+
# numbers = %w{1 2 3}
|
44
|
+
# print [alpha, numbers].together_map do |first, second|
|
45
|
+
# "#{first}:#{second}\n"
|
46
|
+
# end # => output one:1, two:2, three:3
|
47
|
+
#
|
48
|
+
# if you want to multi Array return
|
49
|
+
# alpha = %w{one two three}
|
50
|
+
# numbers = %w{1 2 3}
|
51
|
+
# print [alpha, numbers].together_map do |first, second|
|
52
|
+
# ["#{first}:#{second}", "#{second}:#{first}"]
|
53
|
+
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
54
|
+
def together_map
|
55
|
+
if_not_contain_array_rails_type_error
|
56
|
+
ret = []
|
57
|
+
first.each_with_index do |i_v, i|
|
58
|
+
eval_each_str = get_args_str_for_together i
|
59
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
60
|
+
ret = set_together_each_return_map(ret, each_ret, i)
|
61
|
+
end
|
62
|
+
ret
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
# Arrays together map!.
|
67
|
+
#
|
68
|
+
# together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
|
69
|
+
#
|
70
|
+
# if you want to single Array return
|
71
|
+
# alpha = %w{one two three}
|
72
|
+
# numbers = %w{1 2 3}
|
73
|
+
# ary = [alpha, numbers]
|
74
|
+
# ret = ary.together_map! do |first, second|
|
75
|
+
# "#{first}:#{second}"
|
76
|
+
# end
|
77
|
+
# print ret # => output ['one:1', 'two:2', 'three:3']
|
78
|
+
# print ary # => output ['one:1', 'two:2', 'three:3']
|
79
|
+
#
|
80
|
+
# if you want to multi Array return
|
81
|
+
# alpha = %w{one two three}
|
82
|
+
# numbers = %w{1 2 3}
|
83
|
+
# ary = [alpha, numbers]
|
84
|
+
# ret = ary.together_map! do |first, second|
|
85
|
+
# ["#{first}:#{second}", "#{second}:#{first}"]
|
86
|
+
# end
|
87
|
+
# print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
88
|
+
# print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
89
|
+
def together_map!
|
90
|
+
if_not_contain_array_rails_type_error
|
91
|
+
ret = []
|
92
|
+
first.each_with_index do |i_v, i|
|
93
|
+
eval_each_str = get_args_str_for_together i
|
94
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
95
|
+
ret = set_together_each_return_map(ret, each_ret, i)
|
96
|
+
end
|
97
|
+
clear
|
98
|
+
ret.each { |v|self << v }
|
99
|
+
end
|
100
|
+
|
101
|
+
# Arrays loop together select.
|
102
|
+
#
|
103
|
+
# together_select has aliases [:tselect, :together_find_all, :tfindall]
|
104
|
+
#
|
105
|
+
# if you want to single Array return
|
106
|
+
# firsts = [1, 2, 3, 4]
|
107
|
+
# seconds = [4, 2, 3, 1]
|
108
|
+
# ret = [firsts, seconds].together_select{|first, second|first == second}
|
109
|
+
# print ret # => output [[2, 3], [2, 3]]
|
110
|
+
#
|
111
|
+
# if you want to multi Array return
|
112
|
+
# firsts = [1, 2, 3, 4]
|
113
|
+
# seconds = [4, 2, 3, 1]
|
114
|
+
# ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
|
115
|
+
# print ret # => output [[1, 3], [4, 2]]
|
116
|
+
def together_select
|
117
|
+
if_not_contain_array_rails_type_error
|
118
|
+
ret = []
|
119
|
+
first.each_with_index do |i_v, i|
|
120
|
+
eval_each_str = get_args_str_for_together i
|
121
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
122
|
+
ret = set_together_each_return_select(ret, each_ret, i)
|
123
|
+
end
|
124
|
+
ret
|
125
|
+
end
|
126
|
+
|
127
|
+
# Arrays loop together reduce.
|
128
|
+
#
|
129
|
+
# together_reduce has aliases [:treduce, :together_inject, :tinject]
|
130
|
+
#
|
131
|
+
# if you want to single return
|
132
|
+
# firsts = [1, 2, 3, 4]
|
133
|
+
# seconds = [4, 2, 3, 1]
|
134
|
+
# ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
|
135
|
+
# print ret # => output 20
|
136
|
+
#
|
137
|
+
# if you want to single return with init value
|
138
|
+
# firsts = [1, 2, 3, 4]
|
139
|
+
# seconds = [4, 2, 3, 1]
|
140
|
+
# ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
|
141
|
+
# print ret # => output 30
|
142
|
+
#
|
143
|
+
# if you want to single return with init string value
|
144
|
+
# firsts = %w{a b c}
|
145
|
+
# seconds = %w{1 2 3}
|
146
|
+
# ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
|
147
|
+
# print ret # => output 'start-a1b2c3'
|
148
|
+
#
|
149
|
+
# if you want to single return with init Array value
|
150
|
+
# firsts = [1, 2, 3, 4]
|
151
|
+
# seconds = [4, 2, 3, 1]
|
152
|
+
# ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
|
153
|
+
# print ret # => output [5, 4, 6, 5]
|
154
|
+
#
|
155
|
+
# if you want to single return with init Hash value
|
156
|
+
# firsts = [1, 2, 3, 4]
|
157
|
+
# seconds = [4, 2, 3, 1]
|
158
|
+
# ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
|
159
|
+
# print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
|
160
|
+
def together_reduce(init = nil)
|
161
|
+
if_not_contain_array_rails_type_error
|
162
|
+
memo = initial_memo init
|
163
|
+
first.each_with_index do |i_v, i|
|
164
|
+
eval_each_str = get_args_str_for_together i
|
165
|
+
memo = instance_eval "yield(memo, #{eval_each_str})"
|
166
|
+
end
|
167
|
+
memo
|
168
|
+
end
|
169
|
+
|
170
|
+
# Arrays bulk concat.
|
171
|
+
#
|
172
|
+
# together_concat has alias :tconcat
|
173
|
+
#
|
174
|
+
# alpha = %w{one two three}
|
175
|
+
# numbers = %w{1 2 3}
|
176
|
+
# [alpha, numbers].together do |first, second|
|
177
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
178
|
+
# end
|
179
|
+
def together_concat(other)
|
180
|
+
if_not_contain_array_rails_type_error
|
181
|
+
each { |list|list.concat other }
|
182
|
+
end
|
183
|
+
|
184
|
+
# Arrays bulk at.
|
185
|
+
#
|
186
|
+
# together_at has alias :tat
|
187
|
+
#
|
188
|
+
# same elements size case
|
189
|
+
# alpha = %w{one two three}
|
190
|
+
# numbers = %w{1 2 3}
|
191
|
+
# [alpha, numbers].together_at 2 # => output ['three', 3]
|
192
|
+
#
|
193
|
+
# different elements size case
|
194
|
+
# alpha = %w{one two three}
|
195
|
+
# numbers = %w{1 2}
|
196
|
+
# [alpha, numbers].together_at 2 # => output ['three', nil]
|
197
|
+
def together_at(index)
|
198
|
+
if_not_contain_array_rails_type_error
|
199
|
+
reduce([]) { |ats, list|ats << list.at(index) }
|
200
|
+
end
|
201
|
+
|
202
|
+
# Arrays bulk clear.
|
203
|
+
#
|
204
|
+
# together_clear has alias :tclear
|
205
|
+
#
|
206
|
+
# same elements size case
|
207
|
+
# alpha = %w{one two three}
|
208
|
+
# numbers = %w{1 2 3}
|
209
|
+
# [alpha, numbers].together_clear # => output [[],[]]
|
210
|
+
def together_clear
|
211
|
+
if_not_contain_array_rails_type_error
|
212
|
+
each { |list|list.clear }
|
213
|
+
end
|
214
|
+
|
215
|
+
# Arrays bulk compact.(immutable)
|
216
|
+
#
|
217
|
+
# together_compact has alias :tcompact
|
218
|
+
#
|
219
|
+
# same elements size case
|
220
|
+
# alpha = ['a','b','c', nil,'d']
|
221
|
+
# numbers = [1, 2, nil, 3]
|
222
|
+
# lists = [alpha, numbers]
|
223
|
+
# ret = lists.together_compact
|
224
|
+
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
225
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
226
|
+
def together_compact
|
227
|
+
if_not_contain_array_rails_type_error
|
228
|
+
reduce([]) { |ret, list|ret << list.compact }
|
229
|
+
end
|
230
|
+
|
231
|
+
# Arrays bulk compact!.(mutable)
|
232
|
+
#
|
233
|
+
# together_compact! has alias :tcompact!
|
234
|
+
#
|
235
|
+
# same elements size case
|
236
|
+
# alpha = ['a','b','c', nil,'d']
|
237
|
+
# numbers = [1, 2, nil, 3]
|
238
|
+
# lists = [alpha, numbers]
|
239
|
+
# ret = lists.together_compact!
|
240
|
+
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
241
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
242
|
+
def together_compact!
|
243
|
+
if_not_contain_array_rails_type_error
|
244
|
+
each { |list|list.compact! }
|
245
|
+
end
|
246
|
+
|
247
|
+
# Arrays bulk delete.
|
248
|
+
#
|
249
|
+
# together_delete has alias :tdelete
|
250
|
+
#
|
251
|
+
# if delete target is exist
|
252
|
+
# child1 = [1, 2, 3, 4]
|
253
|
+
# child2 = [2, 3, 4, 5]
|
254
|
+
# lists = [child1, child2]
|
255
|
+
# ret = lists.together_delete 2
|
256
|
+
# print ret # => 2
|
257
|
+
# print lists # => output [[1, 3, 4], [3, 4, 5]]
|
258
|
+
#
|
259
|
+
# if delete target is not exist
|
260
|
+
# child1 = [1, 2, 3, 4]
|
261
|
+
# child2 = [2, 3, 4, 5]
|
262
|
+
# lists = [child1, child2]
|
263
|
+
# ret = lists.together_delete 6
|
264
|
+
# print ret # => nil
|
265
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
266
|
+
#
|
267
|
+
# if delete target is not exist and use block
|
268
|
+
# child1 = [1, 2, 3, 4]
|
269
|
+
# child2 = [2, 3, 4, 5]
|
270
|
+
# lists = [child1, child2]
|
271
|
+
# ret = lists.together_delete(6) { 999 }
|
272
|
+
# print ret # => 999
|
273
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
274
|
+
def together_delete(value)
|
275
|
+
if_not_contain_array_rails_type_error
|
276
|
+
ret = []
|
277
|
+
each { |list|ret << list.delete(value) }
|
278
|
+
default_return = block_given? ? yield : nil
|
279
|
+
ret.compact.size == 0 ? default_return : value
|
280
|
+
end
|
281
|
+
|
282
|
+
private
|
283
|
+
|
284
|
+
def if_not_contain_array_rails_type_error
|
285
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
286
|
+
end
|
287
|
+
|
288
|
+
def get_args_for_together(i)
|
289
|
+
eval_each = []
|
290
|
+
each_with_index { |j_v, j|eval_each << "self[#{j}][#{i}]" }
|
291
|
+
eval_each
|
292
|
+
end
|
293
|
+
|
294
|
+
def get_args_str_for_together(i, with_index = false)
|
295
|
+
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
296
|
+
each_eval.join(',')
|
297
|
+
end
|
298
|
+
|
299
|
+
def set_together_each_return_map(ret, each_ret, index)
|
300
|
+
if together_return_multi?(each_ret)
|
301
|
+
size.times { |i|ret << [] } if index == 0
|
302
|
+
(0..(size - 1)).each { |i|ret[i] << each_ret[i] }
|
303
|
+
else
|
304
|
+
ret << each_ret
|
305
|
+
end
|
306
|
+
ret
|
307
|
+
end
|
308
|
+
|
309
|
+
# def set_together_each_return_map!(ret, each_ret, index)
|
310
|
+
# if together_return_multi?(each_ret)
|
311
|
+
# size.times { |i|ret << [] } if index == 0
|
312
|
+
# (0..(size - 1)).each { |i|ret[i] each_ret[i] }
|
313
|
+
# else
|
314
|
+
# ret << each_ret
|
315
|
+
# end
|
316
|
+
# ret
|
317
|
+
# end
|
318
|
+
|
319
|
+
def set_together_each_return_select(ret, each_ret, index)
|
320
|
+
unless together_return_multi?(each_ret)
|
321
|
+
tmp_each_ret = []
|
322
|
+
size.times { tmp_each_ret << each_ret }
|
323
|
+
each_ret = tmp_each_ret
|
324
|
+
end
|
325
|
+
size.times { |i|ret << [] } if index == 0
|
326
|
+
(0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] }
|
327
|
+
ret
|
328
|
+
end
|
329
|
+
|
330
|
+
def initial_memo(init)
|
331
|
+
return init unless init.nil?
|
332
|
+
first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
|
333
|
+
end
|
334
|
+
|
335
|
+
def together_return_multi?(list)
|
336
|
+
(list.class == Array && list.size == size).to_bool
|
337
|
+
end
|
338
|
+
|
339
|
+
alias_method :tconcat, :together_concat
|
340
|
+
alias_method :tat, :together_at
|
341
|
+
alias_method :tclear, :together_clear
|
342
|
+
alias_method :tcompact, :together_compact
|
343
|
+
alias_method :tcompact!, :together_compact!
|
344
|
+
alias_method :tdelete, :together_delete
|
345
|
+
alias_methods [:together_collect, :tmap, :tcollect], :together_map
|
346
|
+
alias_methods [:together_collect!, :tmap!, :tcollect!], :together_map!
|
347
|
+
alias_methods [:together_find_all, :tselect, :tfindall], :together_select
|
348
|
+
alias_methods [:together_inject, :treduce, :tinject], :together_reduce
|
349
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED