tbpgr_utils 0.0.36 → 0.0.37
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +40 -1
- data/lib/open_classes/array/together.rb +23 -0
- data/lib/open_classes/array/together_at.rb +29 -0
- data/lib/open_classes/array/together_clear.rb +24 -0
- data/lib/open_classes/array/together_compact.rb +44 -0
- data/lib/open_classes/array/together_concat.rb +25 -0
- data/lib/open_classes/array/together_delete.rb +46 -0
- data/lib/open_classes/array/together_delete_at.rb +43 -0
- data/lib/open_classes/array/together_delete_if.rb +38 -0
- data/lib/open_classes/array/together_empty.rb +31 -0
- data/lib/open_classes/array/together_fill.rb +43 -0
- data/lib/open_classes/array/together_first.rb +40 -0
- data/lib/open_classes/array/together_helper.rb +25 -0
- data/lib/open_classes/array/together_include.rb +50 -0
- data/lib/open_classes/array/together_index.rb +34 -0
- data/lib/open_classes/array/together_insert.rb +34 -0
- data/lib/open_classes/array/together_last.rb +40 -0
- data/lib/open_classes/array/together_map.rb +87 -0
- data/lib/open_classes/array/together_pop.rb +47 -0
- data/lib/open_classes/array/together_reduce.rb +59 -0
- data/lib/open_classes/array/together_reverse.rb +53 -0
- data/lib/open_classes/array/together_sample.rb +49 -0
- data/lib/open_classes/array/together_select.rb +50 -0
- data/lib/open_classes/array/together_shift.rb +47 -0
- data/lib/open_classes/array/together_with_index.rb +24 -0
- data/lib/open_classes/array.rb +23 -718
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array/together_at_spec.rb +61 -0
- data/spec/open_classes/array/together_clear_spec.rb +51 -0
- data/spec/open_classes/array/together_compact_spec.rb +103 -0
- data/spec/open_classes/array/together_concat_spec.rb +53 -0
- data/spec/open_classes/array/together_delete_at_spec.rb +78 -0
- data/spec/open_classes/array/together_delete_if_spec.rb +61 -0
- data/spec/open_classes/array/together_delete_spec.rb +80 -0
- data/spec/open_classes/array/together_empty_spec.rb +58 -0
- data/spec/open_classes/array/together_fill_spec.rb +79 -0
- data/spec/open_classes/array/together_first_spec.rb +84 -0
- data/spec/open_classes/array/together_include_spec.rb +100 -0
- data/spec/open_classes/array/together_index_spec.rb +69 -0
- data/spec/open_classes/array/together_insert_spec.rb +65 -0
- data/spec/open_classes/array/together_last_spec.rb +84 -0
- data/spec/open_classes/array/together_map_spec.rb +171 -0
- data/spec/open_classes/array/together_pop_spec.rb +105 -0
- data/spec/open_classes/array/together_reduce_spec.rb +100 -0
- data/spec/open_classes/array/together_reverse_spec.rb +119 -0
- data/spec/open_classes/array/together_select_spec.rb +76 -0
- data/spec/open_classes/array/together_shift_spec.rb +105 -0
- data/spec/open_classes/array/together_spec.rb +51 -0
- data/spec/open_classes/array/together_with_index_spec.rb +51 -0
- data/spec/open_classes/together_sample_spec.rb +122 -0
- data/spec/spec_helper.rb +1 -1
- metadata +82 -14
- data/spec/open_classes/array_spec.rb +0 -1699
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/tbpgr/tbpgr_utils.png?branch=master)](https://travis-ci.org/tbpgr/tbpgr_utils)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/tbpgr/tbpgr_utils/badge.png)](https://coveralls.io/r/tbpgr/tbpgr_utils)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/tbpgr/tbpgr_utils.png)](https://codeclimate.com/github/tbpgr/tbpgr_utils)
|
5
6
|
|
6
7
|
TbpgrUtils is Utilities.
|
7
8
|
|
@@ -45,6 +46,7 @@ Or install it yourself as:
|
|
45
46
|
|[TbpgrUtils Array#together_reduce](#arraytogether_reduceor-treduce-together_inject-tinject) |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
|
46
47
|
|[TbpgrUtils Array#together_reverse](#arraytogether_reverseor-treverse) |together version of Array#reverse. together_reverse has alias :treverse |
|
47
48
|
|[TbpgrUtils Array#together_reverse!](#arraytogether_reverseor-treverse-1) |together version of Array#reverse!. together_reverse! has alias :treverse! |
|
49
|
+
|[TbpgrUtils Array#together_sample](#arraytogether_sampleor-tsample) |together version of Array#sample. together_sample has alias :tsample |
|
48
50
|
|[TbpgrUtils Array#together_select](#arraytogether_selector-tselect-together_find_all-tfindall) |together version of Enumerable#select. together_select has aliases [:tselect, :together_find_all, :tfindall] |
|
49
51
|
|[TbpgrUtils Array#together_shift](#arraytogether_shift) |together version of Array#shift. together_shift has alias :tshift |
|
50
52
|
|[TbpgrUtils Array#together_with_index](#arraytogether_with_index) |loop all arrays by block with index |
|
@@ -628,7 +630,6 @@ print ret # => [[2, 1], []]
|
|
628
630
|
print lists # => [[1, 2], []]
|
629
631
|
~~~
|
630
632
|
|
631
|
-
|
632
633
|
[back to list](#list)
|
633
634
|
|
634
635
|
### Array#together_reverse!(or :treverse!)
|
@@ -652,6 +653,43 @@ print lists # => [[2, 1], []]
|
|
652
653
|
|
653
654
|
[back to list](#list)
|
654
655
|
|
656
|
+
### Array#together_sample(or :tsample)
|
657
|
+
together_sample has alias :tsample
|
658
|
+
|
659
|
+
not empty case
|
660
|
+
~~~ruby
|
661
|
+
lists = [[1, 2], [5, 6]]
|
662
|
+
ret = lists.together_sample
|
663
|
+
print ret # => [1 or 2, 5 or 6]
|
664
|
+
~~~
|
665
|
+
|
666
|
+
empty case
|
667
|
+
~~~ruby
|
668
|
+
lists = [[], []]
|
669
|
+
ret = lists.together_sample
|
670
|
+
print ret # => [nil, nil]
|
671
|
+
~~~
|
672
|
+
|
673
|
+
not empty case with args
|
674
|
+
lists = [[1, 2], [5, 6]]
|
675
|
+
ret = lists.together_sample 2
|
676
|
+
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
|
677
|
+
~~~
|
678
|
+
|
679
|
+
not empty case with args
|
680
|
+
lists = [[], []]
|
681
|
+
ret = lists.together_sample 2
|
682
|
+
print ret # => [[], []]
|
683
|
+
~~~
|
684
|
+
|
685
|
+
not empty, over size case with args
|
686
|
+
lists = [[1, 2], [5, 6]]
|
687
|
+
ret = lists.together_sample 3
|
688
|
+
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
|
689
|
+
~~~
|
690
|
+
|
691
|
+
[back to list](#list)
|
692
|
+
|
655
693
|
### Array#together_select(or tselect, together_find_all, tfindall)
|
656
694
|
~~~ruby
|
657
695
|
require 'tbpgr_utils'
|
@@ -1331,6 +1369,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
1331
1369
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
1332
1370
|
|
1333
1371
|
## History
|
1372
|
+
* version 0.0.37 : add Array#together_sample(alias tsample).
|
1334
1373
|
* version 0.0.36 : add Array#together_reverse,Array#together_reverse!(alias treverse, alias treverse!).
|
1335
1374
|
* version 0.0.35 : add Array#together_pop(alias tpop).
|
1336
1375
|
* version 0.0.34 : add Array#together_last(alias tlast).
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
# Arrays loop together.
|
10
|
+
#
|
11
|
+
# alpha = %w{one two three}
|
12
|
+
# numbers = %w{1 2 3}
|
13
|
+
# [alpha, numbers].together do |first, second|
|
14
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
15
|
+
# end
|
16
|
+
def together
|
17
|
+
if_not_contain_array_rails_type_error
|
18
|
+
first.each_with_index do |i_v, i|
|
19
|
+
eval_each_str = get_args_str_for_together i
|
20
|
+
instance_eval "yield(#{eval_each_str})"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk at.
|
11
|
+
#
|
12
|
+
# together_at has alias :tat
|
13
|
+
#
|
14
|
+
# same elements size case
|
15
|
+
# alpha = %w{one two three}
|
16
|
+
# numbers = %w{1 2 3}
|
17
|
+
# [alpha, numbers].together_at 2 # => output ['three', 3]
|
18
|
+
#
|
19
|
+
# different elements size case
|
20
|
+
# alpha = %w{one two three}
|
21
|
+
# numbers = %w{1 2}
|
22
|
+
# [alpha, numbers].together_at 2 # => output ['three', nil]
|
23
|
+
def together_at(index)
|
24
|
+
if_not_contain_array_rails_type_error
|
25
|
+
reduce([]) { |ats, list|ats << list.at(index) }
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :tat, :together_at
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk clear.
|
11
|
+
#
|
12
|
+
# together_clear has alias :tclear
|
13
|
+
#
|
14
|
+
# same elements size case
|
15
|
+
# alpha = %w{one two three}
|
16
|
+
# numbers = %w{1 2 3}
|
17
|
+
# [alpha, numbers].together_clear # => output [[],[]]
|
18
|
+
def together_clear
|
19
|
+
if_not_contain_array_rails_type_error
|
20
|
+
each { |list|list.clear }
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :tclear, :together_clear
|
24
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk compact.(immutable)
|
11
|
+
#
|
12
|
+
# together_compact has alias :tcompact
|
13
|
+
#
|
14
|
+
# same elements size case
|
15
|
+
# alpha = ['a','b','c', nil,'d']
|
16
|
+
# numbers = [1, 2, nil, 3]
|
17
|
+
# lists = [alpha, numbers]
|
18
|
+
# ret = lists.together_compact
|
19
|
+
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
20
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
21
|
+
def together_compact
|
22
|
+
if_not_contain_array_rails_type_error
|
23
|
+
reduce([]) { |ret, list|ret << list.compact }
|
24
|
+
end
|
25
|
+
|
26
|
+
# Arrays bulk compact!.(mutable)
|
27
|
+
#
|
28
|
+
# together_compact! has alias :tcompact!
|
29
|
+
#
|
30
|
+
# same elements size case
|
31
|
+
# alpha = ['a','b','c', nil,'d']
|
32
|
+
# numbers = [1, 2, nil, 3]
|
33
|
+
# lists = [alpha, numbers]
|
34
|
+
# ret = lists.together_compact!
|
35
|
+
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
36
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
37
|
+
def together_compact!
|
38
|
+
if_not_contain_array_rails_type_error
|
39
|
+
each { |list|list.compact! }
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :tcompact, :together_compact
|
43
|
+
alias_method :tcompact!, :together_compact!
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk concat.
|
11
|
+
#
|
12
|
+
# together_concat has alias :tconcat
|
13
|
+
#
|
14
|
+
# alpha = %w{one two three}
|
15
|
+
# numbers = %w{1 2 3}
|
16
|
+
# [alpha, numbers].together do |first, second|
|
17
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
18
|
+
# end
|
19
|
+
def together_concat(other)
|
20
|
+
if_not_contain_array_rails_type_error
|
21
|
+
each { |list|list.concat other }
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method :tconcat, :together_concat
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk delete.
|
11
|
+
#
|
12
|
+
# together_delete has alias :tdelete
|
13
|
+
#
|
14
|
+
# if delete target is exist
|
15
|
+
# child1 = [1, 2, 3, 4]
|
16
|
+
# child2 = [2, 3, 4, 5]
|
17
|
+
# lists = [child1, child2]
|
18
|
+
# ret = lists.together_delete 2
|
19
|
+
# print ret # => 2
|
20
|
+
# print lists # => output [[1, 3, 4], [3, 4, 5]]
|
21
|
+
#
|
22
|
+
# if delete target is not exist
|
23
|
+
# child1 = [1, 2, 3, 4]
|
24
|
+
# child2 = [2, 3, 4, 5]
|
25
|
+
# lists = [child1, child2]
|
26
|
+
# ret = lists.together_delete 6
|
27
|
+
# print ret # => nil
|
28
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
29
|
+
#
|
30
|
+
# if delete target is not exist and use block
|
31
|
+
# child1 = [1, 2, 3, 4]
|
32
|
+
# child2 = [2, 3, 4, 5]
|
33
|
+
# lists = [child1, child2]
|
34
|
+
# ret = lists.together_delete(6) { 999 }
|
35
|
+
# print ret # => 999
|
36
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
37
|
+
def together_delete(value)
|
38
|
+
if_not_contain_array_rails_type_error
|
39
|
+
ret = []
|
40
|
+
each { |list|ret << list.delete(value) }
|
41
|
+
default_return = block_given? ? yield : nil
|
42
|
+
ret.compact.size == 0 ? default_return : value
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :tdelete, :together_delete
|
46
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk delete_at.
|
11
|
+
#
|
12
|
+
# together_delete_at has alias :tdelete_at
|
13
|
+
#
|
14
|
+
# if delete_at target is exist
|
15
|
+
# child1 = [1, 2, 3, 4]
|
16
|
+
# child2 = [2, 3, 4, 5]
|
17
|
+
# lists = [child1, child2]
|
18
|
+
# ret = lists.together_delete_at 2
|
19
|
+
# print ret # => [3, 4]
|
20
|
+
# print lists # => output [[1, 2, 4], [2, 3, 5]]
|
21
|
+
#
|
22
|
+
# if delete_at target is not exist
|
23
|
+
# child1 = [1, 2, 3, 4]
|
24
|
+
# child2 = [2, 3, 4, 5]
|
25
|
+
# lists = [child1, child2]
|
26
|
+
# ret = lists.together_delete_at 6
|
27
|
+
# print ret # => [nil, nil]
|
28
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
29
|
+
#
|
30
|
+
# if delete_at target is exist(minus index)
|
31
|
+
# child1 = [1, 2, 3, 4]
|
32
|
+
# child2 = [2, 3, 4, 5]
|
33
|
+
# lists = [child1, child2]
|
34
|
+
# ret = lists.together_delete_at -3
|
35
|
+
# print ret # => [2, 3]
|
36
|
+
# print lists # => output [[1, 3, 4], [2, 4, 5]]
|
37
|
+
def together_delete_at(index)
|
38
|
+
if_not_contain_array_rails_type_error
|
39
|
+
reduce([]) { |ret, list|ret << list.delete_at(index) }
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :tdelete_at, :together_delete_at
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk delete_if.
|
11
|
+
#
|
12
|
+
# together_delete_if has alias :tdelete_if
|
13
|
+
#
|
14
|
+
# if delete_if target is exist. return self.
|
15
|
+
# lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
|
16
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
17
|
+
# print ret # => [[2, 4], [4, 8]]
|
18
|
+
#
|
19
|
+
# if delete_if target is not exist. return nil.
|
20
|
+
# lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
|
21
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
22
|
+
# print ret # => nil
|
23
|
+
def together_delete_if(&block)
|
24
|
+
if_not_contain_array_rails_type_error
|
25
|
+
have_deleted = false
|
26
|
+
first.each_with_index do |i_v, i|
|
27
|
+
eval_each_str = get_args_str_for_together i
|
28
|
+
is_delete = instance_eval "yield(#{eval_each_str})"
|
29
|
+
if is_delete
|
30
|
+
each { |e|e.delete_at i }
|
31
|
+
have_deleted = true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
have_deleted ? self : nil
|
35
|
+
end
|
36
|
+
|
37
|
+
alias_method :tdelete_if, :together_delete_if
|
38
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk empty?.
|
11
|
+
#
|
12
|
+
# together_empty? has alias :tempty?
|
13
|
+
#
|
14
|
+
# empty case
|
15
|
+
# lists = [[], []]
|
16
|
+
# ret = lists.together_empty?
|
17
|
+
# print ret # => true
|
18
|
+
#
|
19
|
+
# not empty case
|
20
|
+
# lists = [[1], []]
|
21
|
+
# ret = lists.together_empty?
|
22
|
+
# print ret # => false
|
23
|
+
def together_empty?
|
24
|
+
if_not_contain_array_rails_type_error
|
25
|
+
is_empty = true
|
26
|
+
each { |list|is_empty = is_empty && list.empty? }
|
27
|
+
is_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :tempty?, :together_empty?
|
31
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk fill.
|
11
|
+
#
|
12
|
+
# together_fill has alias :tfill
|
13
|
+
#
|
14
|
+
# not use block case
|
15
|
+
# lists = [[*1..5], [*6..10]]
|
16
|
+
# ret = lists.together_fill(99)
|
17
|
+
# print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]
|
18
|
+
#
|
19
|
+
# use block, no args case
|
20
|
+
# lists = [[*1..5], [*6..10]]
|
21
|
+
# ret = lists.together_fill { |i|(i + 1) + 1 }
|
22
|
+
# print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
|
23
|
+
#
|
24
|
+
# use block, has args case
|
25
|
+
# lists = [[*1..5], [*6..10]]
|
26
|
+
# ret = lists.together_fill(2) { |i|(i + 1) + 1 }
|
27
|
+
# print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]
|
28
|
+
def together_fill(fill_value = nil, &block)
|
29
|
+
if_not_contain_array_rails_type_error
|
30
|
+
if block
|
31
|
+
fill_value = 0 if fill_value.nil?
|
32
|
+
first.each_with_index do |i_v, i|
|
33
|
+
next if i < fill_value
|
34
|
+
each { |list|list[i] = yield(i) }
|
35
|
+
end
|
36
|
+
else
|
37
|
+
each { |list|list.fill fill_value }
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :tfill, :together_fill
|
43
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk first.
|
11
|
+
#
|
12
|
+
# together_first has alias :tfirst
|
13
|
+
#
|
14
|
+
# no args case
|
15
|
+
# lists = [[*1..5], [*6..10]]
|
16
|
+
# ret = lists.together_first
|
17
|
+
# print ret # => [1, 6]
|
18
|
+
#
|
19
|
+
# has args 2 case
|
20
|
+
# lists = [[*1..5], [*6..10]]
|
21
|
+
# ret = lists.together_first 2
|
22
|
+
# print ret # => [[1, 2], [6, 7]]
|
23
|
+
#
|
24
|
+
# has args 0 case
|
25
|
+
# lists = [[*1..5], [*6..10]]
|
26
|
+
# ret = lists.together_first 0
|
27
|
+
# print ret # => [[], []]
|
28
|
+
#
|
29
|
+
# has args over size case
|
30
|
+
# lists = [[*1..5], [*6..10]]
|
31
|
+
# ret = lists.together_first 6
|
32
|
+
# print ret # => [[*1..5], [*6..10]]
|
33
|
+
def together_first(index = nil)
|
34
|
+
if_not_contain_array_rails_type_error
|
35
|
+
each_return = index == 0 ? '[]' : index.nil? ? 'list.first' : 'list.first(index)'
|
36
|
+
reduce([]) { |ret, list|ret << eval(each_return, binding) }
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method :tfirst, :together_first
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
|
5
|
+
# TogetherHelper
|
6
|
+
module TogetherHelper
|
7
|
+
def if_not_contain_array_rails_type_error
|
8
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_args_for_together(i)
|
12
|
+
eval_each = []
|
13
|
+
each_with_index { |j_v, j|eval_each << "self[#{j}][#{i}]" }
|
14
|
+
eval_each
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_args_str_for_together(i, with_index = false)
|
18
|
+
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
19
|
+
each_eval.join(',')
|
20
|
+
end
|
21
|
+
|
22
|
+
def together_return_multi?(list)
|
23
|
+
(list.class == Array && list.size == size).to_bool
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk include?.
|
11
|
+
#
|
12
|
+
# together_include? has alias :tinclude?
|
13
|
+
#
|
14
|
+
# both include single ret case
|
15
|
+
# lists = [[*1..5], [*5..9]]
|
16
|
+
# ret = lists.together_include? 5
|
17
|
+
# print ret # => true
|
18
|
+
#
|
19
|
+
# one include single ret case
|
20
|
+
# lists = [[*1..5], [*5..9]]
|
21
|
+
# ret = lists.together_include? 9
|
22
|
+
# print ret # => true
|
23
|
+
#
|
24
|
+
# both not include single ret case
|
25
|
+
# lists = [[*1..5], [*5..9]]
|
26
|
+
# ret = lists.together_include? 10
|
27
|
+
# print ret # => false
|
28
|
+
#
|
29
|
+
# both include multi ret case
|
30
|
+
# lists = [[*1..5], [*5..9]]
|
31
|
+
# ret = lists.together_include? 5, true
|
32
|
+
# print ret # => [true, true]
|
33
|
+
#
|
34
|
+
# one include multi ret case
|
35
|
+
# lists = [[*1..5], [*5..9]]
|
36
|
+
# ret = lists.together_include? 9, true
|
37
|
+
# print ret # => [false, true]
|
38
|
+
#
|
39
|
+
# both not include multi ret case
|
40
|
+
# lists = [[*1..5], [*5..9]]
|
41
|
+
# ret = lists.together_include? 10, true
|
42
|
+
# print ret # => [false, false]
|
43
|
+
def together_include?(value, is_multi = false)
|
44
|
+
if_not_contain_array_rails_type_error
|
45
|
+
return reduce([]) { |ret, list|ret << list.include?(value) } if is_multi
|
46
|
+
reduce(false) { |ret, list|ret = ret || list.include?(value) }
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :tinclude?, :together_include?
|
50
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk index.
|
11
|
+
#
|
12
|
+
# together_index has alias :tindex
|
13
|
+
#
|
14
|
+
# both index exist case
|
15
|
+
# lists = [[*1..5], [*5..9]]
|
16
|
+
# ret = lists.together_index 5
|
17
|
+
# print ret # => [4, 0]
|
18
|
+
#
|
19
|
+
# one include single ret case
|
20
|
+
# lists = [[*1..5], [*5..9]]
|
21
|
+
# ret = lists.together_index 4
|
22
|
+
# print ret # => [3, nil]
|
23
|
+
#
|
24
|
+
# both not include single ret case
|
25
|
+
# lists = [[*1..5], [*5..9]]
|
26
|
+
# ret = lists.together_index 10
|
27
|
+
# print ret # => [nil, nil]
|
28
|
+
def together_index(value)
|
29
|
+
if_not_contain_array_rails_type_error
|
30
|
+
reduce([]) { |ret, list|ret << list.index(value) }
|
31
|
+
end
|
32
|
+
|
33
|
+
alias_method :tindex, :together_index
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk insert.
|
11
|
+
#
|
12
|
+
# together_insert has alias :tinsert
|
13
|
+
#
|
14
|
+
# both insert exist case
|
15
|
+
# lists = [[*1..5], [*5..9]]
|
16
|
+
# ret = lists.together_insert(1, 55, 66)
|
17
|
+
# print ret # => [[1, 55, 66, 2, 3, 4, 5], [5, 55, 66, 6, 7, 8, 9]]
|
18
|
+
#
|
19
|
+
# both insert exist and minus index case
|
20
|
+
# lists = [[*1..5], [*5..9]]
|
21
|
+
# ret = lists.together_insert(-2, 55, 66)
|
22
|
+
# print ret # => [[1, 2, 3, 4, 55, 66, 5], [5, 6, 7, 8, 55, 66, 9]]
|
23
|
+
#
|
24
|
+
# both insert exist case
|
25
|
+
# lists = [[*1..5], [*5..9]]
|
26
|
+
# ret = lists.together_insert(6, 55, 66)
|
27
|
+
# print ret # => [[1, 2, 3, 4, 5, nil, 55, 66], [5, 6, 7, 8, 9, nil, 55, 66]],
|
28
|
+
def together_insert(index, *args)
|
29
|
+
if_not_contain_array_rails_type_error
|
30
|
+
each { |list|list.insert(index, *args) }
|
31
|
+
end
|
32
|
+
|
33
|
+
alias_method :tinsert, :together_insert
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
require 'open_classes/array/together_helper'
|
5
|
+
|
6
|
+
# Array
|
7
|
+
class Array
|
8
|
+
include TogetherHelper
|
9
|
+
|
10
|
+
# Arrays bulk last.
|
11
|
+
#
|
12
|
+
# together_last has alias :tlast
|
13
|
+
#
|
14
|
+
# no args case
|
15
|
+
# lists = [[*1..5], [*6..10]]
|
16
|
+
# ret = lists.together_last
|
17
|
+
# print ret # => [5, 10]
|
18
|
+
#
|
19
|
+
# has args 2 case
|
20
|
+
# lists = [[*1..5], [*6..10]]
|
21
|
+
# ret = lists.together_last 2
|
22
|
+
# print ret # => [[4, 5], [9, 10]]
|
23
|
+
#
|
24
|
+
# has args 0 case
|
25
|
+
# lists = [[*1..5], [*6..10]]
|
26
|
+
# ret = lists.together_last 0
|
27
|
+
# print ret # => [[], []]
|
28
|
+
#
|
29
|
+
# has args over size case
|
30
|
+
# lists = [[*1..5], [*6..10]]
|
31
|
+
# ret = lists.together_last 6
|
32
|
+
# print ret # => [[*1..5], [*6..10]]
|
33
|
+
def together_last(index = nil)
|
34
|
+
if_not_contain_array_rails_type_error
|
35
|
+
each_return = index == 0 ? '[]' : index.nil? ? 'list.last' : 'list.last(index)'
|
36
|
+
reduce([]) { |ret, list|ret << eval(each_return, binding) }
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method :tlast, :together_last
|
40
|
+
end
|