Class: Array

Inherits:
Object show all
Includes:
TogetherHelper
Defined in:
lib/open_classes/array/sum.rb,
lib/open_classes/array/average.rb,
lib/open_classes/array/gte_gte.rb,
lib/open_classes/array/exchange.rb,
lib/open_classes/array/to_table.rb,
lib/open_classes/array/together.rb,
lib/open_classes/array/uniq_size.rb,
lib/open_classes/array/kernel_send.rb,
lib/open_classes/array/together_at.rb,
lib/open_classes/array/together_map.rb,
lib/open_classes/array/together_pop.rb,
lib/open_classes/array/to_html_table.rb,
lib/open_classes/array/together_fill.rb,
lib/open_classes/array/together_last.rb,
lib/open_classes/array/together_clear.rb,
lib/open_classes/array/together_empty.rb,
lib/open_classes/array/together_first.rb,
lib/open_classes/array/together_index.rb,
lib/open_classes/array/together_shift.rb,
lib/open_classes/array/together_slice.rb,
lib/open_classes/array/together_concat.rb,
lib/open_classes/array/together_delete.rb,
lib/open_classes/array/together_insert.rb,
lib/open_classes/array/together_reduce.rb,
lib/open_classes/array/together_sample.rb,
lib/open_classes/array/together_select.rb,
lib/open_classes/array/together_compact.rb,
lib/open_classes/array/together_include.rb,
lib/open_classes/array/together_reverse.rb,
lib/open_classes/array/together_shuffle.rb,
lib/open_classes/array/together_delete_at.rb,
lib/open_classes/array/together_delete_if.rb,
lib/open_classes/array/together_with_index.rb

Overview

Array

Instance Method Summary collapse

Methods included from TogetherHelper

#get_args_for_together, #get_args_str_for_together, #if_not_contain_array_rails_type_error, #together_return_multi?

Instance Method Details

#>>(dummy = nil) ⇒ Object

return ArrayContext for each execute

Example

[*'a'..'c'].>>.ord # => [97, 98, 99]
[*'a'..'c'].>>.'ord' # => [97, 98, 99]
[*'aa'..'ac'].>>.gsub("a", "c") # => ['cc', 'cb', 'cc']


37
38
39
# File 'lib/open_classes/array/gte_gte.rb', line 37

def >>(dummy = nil)
  ArrayContext.new(self)
end

#averageObject

return average

Example

[*1..6].average # => 3.5
[1.5, 2.5].average # => 2.0
[*'a'..'z'].average # => raise TypeError


13
14
15
16
# File 'lib/open_classes/array/average.rb', line 13

def average
  fail TypeError, 'you have to use elements that is Numeric' if any? { |v|!v.is_a? Numeric }
  reduce(0.0) { |r, v|r = r + v.to_f; r } / size
end

#exchange!(one_index, other_index) ⇒ Object

exchange arrays elements

Example

[*1..6].exchange!(1, 5) # => [1, 6, 3, 4, 5, 2]
[*1..6].exchange!(1, -1) # => [1, 6, 3, 4, 5, 2]
[*1..6].exchange!(1, 6) # => [*1..6]
[].exchange!(1, 2) # => []


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/open_classes/array/exchange.rb', line 14

def exchange!(one_index, other_index)
  return self unless one_index.respond_to? :to_i
  return self unless other_index.respond_to? :to_i
  one_index = one_index.to_i
  other_index = other_index.to_i
  return self if one_index.abs >= size
  return self if other_index.abs >= size
  tmp_one = self[one_index]
  self[one_index] = self[other_index]
  self[other_index] = tmp_one
  self
end

#initial_memo(init) ⇒ Object



53
54
55
56
# File 'lib/open_classes/array/together_reduce.rb', line 53

def initial_memo(init)
  return init unless init.nil?
  first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
end

#kernel_send(method_name) ⇒ Object

alias of map {|v|send :some_kernel_method, v}

Example

[*1..3].kernel_send:Rational # => [(1/1), (2/1), (3/1)]


11
12
13
14
# File 'lib/open_classes/array/kernel_send.rb', line 11

def kernel_send(method_name)
  return self unless [Symbol, String].include? method_name.class
  map { |v|send method_name.to_sym, v }
end

#sumObject

alias of Array#reduce(&:+)

Example

[*1..5].reduce(&:+) # => 15
[*'a'..'z'].reduce(&:+) # => 'abcdefghijklmnopqrstuvwxyz'


12
13
14
# File 'lib/open_classes/array/sum.rb', line 12

def sum
  reduce(&:+)
end

#to_html_table(options = { no_header: false }) ⇒ Object

Array(Array, Array…) to html table format.

Example

[['header1', 'header2', 'header3'],['line1_1', 'line1_2', 'line1_3']].to_html_table

result

<table>
  <tr>
    <th>header1</th>
    <th>header2</th>
    <th>header3</th>
  </tr>
  <tr>
    <td>line1_1</td>
    <td>line1_2</td>
    <td>line1_3</td>
  </tr>
</table>


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/open_classes/array/to_html_table.rb', line 26

def to_html_table(options = { no_header: false })
  options[:no_header] = false if options[:no_header].nil?
  cnt = options[:no_header] ? 1 : 0
  ret = reduce(['<table>']) do |rets, lines|
    ret = lines.reduce([]) do |ret, column|
      tag = (cnt == 0) ? 'th' : 'td'
      ret << "    <#{tag}>#{column}</#{tag}>"
      ret
    end
    cnt += 1
    inner_tr = ret.join("\n")
    rets << "  <tr>\n#{inner_tr}\n  </tr>"
  end.join("\n") + "\n</table>\n"
end

#to_table(position = :right) ⇒ Object

Array(Array, Array…) to table format.

Example

[['header1', 'header2', 'header3'],['line1_1', 'line1_2', 'line1_3']].to_table

result

|header1|header2|header3|
|line1_1|line1_2|line1_3|


16
17
18
19
20
21
22
# File 'lib/open_classes/array/to_table.rb', line 16

def to_table(position = :right)
  ret = reduce([]) do |rets, lines|
    ret = lines.reduce([]) { |ret, column|ret << column; ret }
    rets << "|#{ret.join('|')}|"
  end.join("\n") + "\n"
  ret.justify_table(position)
end

#togetherObject

Arrays loop together.

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together do |first, second|
  print "#{first}:#{second}\n"  # => output one:1, two:2, three:3
end


16
17
18
19
20
21
22
# File 'lib/open_classes/array/together.rb', line 16

def together
  if_not_contain_array_rails_type_error
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    instance_eval "yield(#{eval_each_str})"
  end
end

#together_at(index) ⇒ Object Also known as: tat

Arrays bulk at.

together_at has alias :tat

same elements size case

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_at 2 # => output ['three', 3]

different elements size case

alpha = %w{one two three}
numbers = %w{1 2}
[alpha, numbers].together_at 2 # => output ['three', nil]


23
24
25
26
# File 'lib/open_classes/array/together_at.rb', line 23

def together_at(index)
  if_not_contain_array_rails_type_error
  reduce([]) { |ats, list|ats << list.at(index) }
end

#together_clearObject Also known as: tclear

Arrays bulk clear.

together_clear has alias :tclear

same elements size case

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_clear # => output [[],[]]


18
19
20
21
# File 'lib/open_classes/array/together_clear.rb', line 18

def together_clear
  if_not_contain_array_rails_type_error
  each { |list|list.clear }
end

#together_compactObject Also known as: tcompact

Arrays bulk compact.(immutable)

together_compact has alias :tcompact

same elements size case

alpha = ['a','b','c', nil,'d']
numbers = [1, 2, nil, 3]
lists = [alpha, numbers]
ret = lists.together_compact
print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
print ret # => output [['a','b','c','d'], [1, 2, 3]]


21
22
23
24
# File 'lib/open_classes/array/together_compact.rb', line 21

def together_compact
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.compact }
end

#together_compact!Object Also known as: tcompact!

Arrays bulk compact!.(mutable)

together_compact! has alias :tcompact!

same elements size case

alpha = ['a','b','c', nil,'d']
numbers = [1, 2, nil, 3]
lists = [alpha, numbers]
ret = lists.together_compact!
print lists # => output [['a','b','c','d'], [1, 2, 3]]
print ret # => output [['a','b','c','d'], [1, 2, 3]]


37
38
39
40
# File 'lib/open_classes/array/together_compact.rb', line 37

def together_compact!
  if_not_contain_array_rails_type_error
  each { |list|list.compact! }
end

#together_concat(other) ⇒ Object Also known as: tconcat

Arrays bulk concat.

together_concat has alias :tconcat

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together do |first, second|
  print "#{first}:#{second}\n"  # => output one:1, two:2, three:3
end


19
20
21
22
# File 'lib/open_classes/array/together_concat.rb', line 19

def together_concat(other)
  if_not_contain_array_rails_type_error
  each { |list|list.concat other }
end

#together_delete(value) ⇒ Object Also known as: tdelete

Arrays bulk delete.

together_delete has alias :tdelete

if delete target is exist

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete 2
print ret # => 2
print lists # => output [[1, 3, 4], [3, 4, 5]]

if delete target is not exist

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete 6
print ret # => nil
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]

if delete target is not exist and use block

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete(6) { 999 }
print ret # => 999
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]


37
38
39
40
41
42
43
# File 'lib/open_classes/array/together_delete.rb', line 37

def together_delete(value)
  if_not_contain_array_rails_type_error
  ret = []
  each { |list|ret << list.delete(value) }
  default_return = block_given? ? yield : nil
  ret.compact.size == 0 ? default_return : value
end

#together_delete_at(index) ⇒ Object Also known as: tdelete_at

Arrays bulk delete_at.

together_delete_at has alias :tdelete_at

if delete_at target is exist

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at 2
print ret # => [3, 4]
print lists # => output [[1, 2, 4], [2, 3, 5]]

if delete_at target is not exist

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at 6
print ret # => [nil, nil]
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]

if delete_at target is exist(minus index)

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at -3
print ret # => [2, 3]
print lists # => output [[1, 3, 4], [2, 4, 5]]


37
38
39
40
# File 'lib/open_classes/array/together_delete_at.rb', line 37

def together_delete_at(index)
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.delete_at(index) }
end

#together_delete_if(&block) ⇒ Object Also known as: tdelete_if

Arrays bulk delete_if.

together_delete_if has alias :tdelete_if

if delete_if target is exist. return self.

lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
ret = lists.together_delete_if {|first, second|(first + second).odd?}
print ret # => [[2, 4], [4, 8]]

if delete_if target is not exist. return nil.

lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
ret = lists.together_delete_if {|first, second|(first + second).odd?}
print ret # => nil


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/open_classes/array/together_delete_if.rb', line 23

def together_delete_if(&block)
  if_not_contain_array_rails_type_error
  have_deleted = false
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    is_delete = instance_eval "yield(#{eval_each_str})"
    if is_delete
      each { |e|e.delete_at i }
      have_deleted = true
    end
  end
  have_deleted ? self : nil
end

#together_empty?Boolean Also known as: tempty?

Arrays bulk empty?.

together_empty? has alias :tempty?

empty case

lists = [[], []]
ret = lists.together_empty?
print ret # => true

not empty case

lists = [[1], []]
ret = lists.together_empty?
print ret # => false

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/open_classes/array/together_empty.rb', line 23

def together_empty?
  if_not_contain_array_rails_type_error
  is_empty = true
  each { |list|is_empty = is_empty && list.empty? }
  is_empty
end

#together_fill(fill_value = nil, &block) ⇒ Object Also known as: tfill

Arrays bulk fill.

together_fill has alias :tfill

not use block case

lists = [[*1..5], [*6..10]]
ret = lists.together_fill(99)
print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]

use block, no args case

lists = [[*1..5], [*6..10]]
ret = lists.together_fill { |i|(i + 1) + 1 }
print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]

use block, has args case

lists = [[*1..5], [*6..10]]
ret = lists.together_fill(2) { |i|(i + 1) + 1 }
print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/open_classes/array/together_fill.rb', line 28

def together_fill(fill_value = nil, &block)
  if_not_contain_array_rails_type_error
  if block
    fill_value = 0 if fill_value.nil?
    first.each_with_index do |i_v, i|
      next if i < fill_value
      each { |list|list[i] = yield(i) }
    end
  else
    each { |list|list.fill fill_value }
  end
  self
end

#together_first(index = nil) ⇒ Object Also known as: tfirst

Arrays bulk first.

together_first has alias :tfirst

no args case

lists = [[*1..5], [*6..10]]
ret = lists.together_first
print ret # => [1, 6]

has args 2 case

lists = [[*1..5], [*6..10]]
ret = lists.together_first 2
print ret # => [[1, 2], [6, 7]]

has args 0 case

lists = [[*1..5], [*6..10]]
ret = lists.together_first 0
print ret # => [[], []]

has args over size case

lists = [[*1..5], [*6..10]]
ret = lists.together_first 6
print ret # => [[*1..5], [*6..10]]


33
34
35
36
37
# File 'lib/open_classes/array/together_first.rb', line 33

def together_first(index = nil)
  if_not_contain_array_rails_type_error
  each_return = index == 0 ? '[]' : index.nil? ? 'list.first' : 'list.first(index)'
  reduce([]) { |ret, list|ret << eval(each_return, binding) }
end

#together_include?(value, is_multi = false) ⇒ Boolean Also known as: tinclude?

Arrays bulk include?.

together_include? has alias :tinclude?

both include single ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 5
print ret # => true

one include single ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 9
print ret # => true

both not include single ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 10
print ret # => false

both include multi ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 5, true
print ret # => [true, true]

one include multi ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 9, true
print ret # => [false, true]

both not include multi ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 10, true
print ret # => [false, false]

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/open_classes/array/together_include.rb', line 43

def together_include?(value, is_multi = false)
  if_not_contain_array_rails_type_error
  return reduce([]) { |ret, list|ret << list.include?(value) } if is_multi
  reduce(false) { |ret, list|ret = ret || list.include?(value) }
end

#together_index(value) ⇒ Object Also known as: tindex

Arrays bulk index.

together_index has alias :tindex

both index exist case

lists = [[*1..5], [*5..9]]
ret = lists.together_index 5
print ret # => [4, 0]

one include single ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_index 4
print ret # => [3, nil]

both not include single ret case

lists = [[*1..5], [*5..9]]
ret = lists.together_index 10
print ret # => [nil, nil]


28
29
30
31
# File 'lib/open_classes/array/together_index.rb', line 28

def together_index(value)
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.index(value) }
end

#together_insert(index, *args) ⇒ Object Also known as: tinsert

Arrays bulk insert.

together_insert has alias :tinsert

both insert exist case

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(1, 55, 66)
print ret # => [[1, 55, 66, 2, 3, 4, 5], [5, 55, 66, 6, 7, 8, 9]]

both insert exist and minus index case

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(-2, 55, 66)
print ret # => [[1, 2, 3, 4, 55, 66, 5], [5, 6, 7, 8, 55, 66, 9]]

both insert exist case

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(6, 55, 66)
print ret # => [[1, 2, 3, 4, 5, nil, 55, 66], [5, 6, 7, 8, 9, nil, 55, 66]],


28
29
30
31
# File 'lib/open_classes/array/together_insert.rb', line 28

def together_insert(index, *args)
  if_not_contain_array_rails_type_error
  each { |list|list.insert(index, *args) }
end

#together_last(index = nil) ⇒ Object Also known as: tlast

Arrays bulk last.

together_last has alias :tlast

no args case

lists = [[*1..5], [*6..10]]
ret = lists.together_last
print ret # => [5, 10]

has args 2 case

lists = [[*1..5], [*6..10]]
ret = lists.together_last 2
print ret # => [[4, 5], [9, 10]]

has args 0 case

lists = [[*1..5], [*6..10]]
ret = lists.together_last 0
print ret # => [[], []]

has args over size case

lists = [[*1..5], [*6..10]]
ret = lists.together_last 6
print ret # => [[*1..5], [*6..10]]


33
34
35
36
37
# File 'lib/open_classes/array/together_last.rb', line 33

def together_last(index = nil)
  if_not_contain_array_rails_type_error
  each_return = index == 0 ? '[]' : index.nil? ? 'list.last' : 'list.last(index)'
  reduce([]) { |ret, list|ret << eval(each_return, binding) }
end

#together_mapObject

Arrays together map.

together_map has aliases [:tmap, :together_collect, :tcollect]

if you want to single Array return

alpha = %w{one two three}
numbers = %w{1 2 3}
print [alpha, numbers].together_map do |first, second|
  "#{first}:#{second}\n"
end # => output one:1, two:2, three:3

if you want to multi Array return

alpha = %w{one two three}
numbers = %w{1 2 3}
print [alpha, numbers].together_map do |first, second|
  ["#{first}:#{second}", "#{second}:#{first}"]
end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]


27
28
29
30
31
32
33
34
35
36
# File 'lib/open_classes/array/together_map.rb', line 27

def together_map
  if_not_contain_array_rails_type_error
  ret = []
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    each_ret = instance_eval "yield(#{eval_each_str})"
    ret = set_together_each_return_map(ret, each_ret, i)
  end
  ret
end

#together_map!Object

Arrays together map!.

together_map! has aliases [:tmap!, :together_collect!, :tcollect!]

if you want to single Array return

alpha = %w{one two three}
numbers = %w{1 2 3}
ary = [alpha, numbers]
ret = ary.together_map! do |first, second|
  "#{first}:#{second}"
end
print ret # => output ['one:1', 'two:2', 'three:3']
print ary # => output ['one:1', 'two:2', 'three:3']

if you want to multi Array return

alpha = %w{one two three}
numbers = %w{1 2 3}
ary = [alpha, numbers]
ret = ary.together_map! do |first, second|
  ["#{first}:#{second}", "#{second}:#{first}"]
end
print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/open_classes/array/together_map.rb', line 61

def together_map!
  if_not_contain_array_rails_type_error
  ret = []
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    each_ret = instance_eval "yield(#{eval_each_str})"
    ret = set_together_each_return_map(ret, each_ret, i)
  end
  clear
  ret.each { |v|self << v }
end

#together_pop(count = nil) ⇒ Object Also known as: tpop

Arrays bulk pop.

together_pop has alias :tpop

not empty case

lists = [[1, 2], [5, 6]]
ret = lists.together_pop
print ret # => [2, 6]
print lists # => [1, 5]

empty case

lists = [[], []]
ret = lists.together_pop
print ret # => [nil, nil]
print lists # => [[], []]

not empty case with args

lists = [[1, 2], [5, 6]]
ret = lists.together_pop 2
print ret # => [[1, 2], [5, 6]]
print lists # => [[], []]

not empty case with args

lists = [[], []]
ret = lists.together_pop 2
print ret # => [[], []]
print lists # => [[], []]


37
38
39
40
41
42
43
44
# File 'lib/open_classes/array/together_pop.rb', line 37

def together_pop(count = nil)
  if_not_contain_array_rails_type_error
  if count.nil?
    reduce([]) { |ret, list|ret << list.pop }
  else
    reduce([]) { |ret, list|ret << list.pop(count) }
  end
end

#together_reduce(init = nil) ⇒ Object

Arrays loop together reduce.

together_reduce has aliases [:treduce, :together_inject, :tinject]

if you want to single return

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
print ret # => output  20

if you want to single return with init value

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
print ret # => output  30

if you want to single return with init string value

firsts = %w{a b c}
seconds =  %w{1 2 3}
ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
print ret # => output 'start-a1b2c3'

if you want to single return with init Array value

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
print ret # => output [5, 4, 6, 5]

if you want to single return with init Hash value

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}


43
44
45
46
47
48
49
50
51
# File 'lib/open_classes/array/together_reduce.rb', line 43

def together_reduce(init = nil)
  if_not_contain_array_rails_type_error
  memo = initial_memo init
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    memo = instance_eval "yield(memo, #{eval_each_str})"
  end
  memo
end

#together_reverseObject Also known as: treverse

Arrays bulk reverse.

together_reverse has alias :treverse

not empty case

lists = [[1, 2], [5, 6]]
ret = lists.together_reverse
print ret # => [[2, 1], [6, 5]]
print lists # => [[1, 2], [5, 6]]

one empty case

lists = [[1, 2], []]
ret = lists.together_reverse
print ret # => [[2, 1], []]
print lists # => [[1, 2], []]


25
26
27
28
# File 'lib/open_classes/array/together_reverse.rb', line 25

def together_reverse
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.reverse }
end

#together_reverse!Object Also known as: treverse!

Arrays bulk reverse!.

together_reverse! has alias :treverse!

not empty case

lists = [[1, 2], [5, 6]]
ret = lists.together_reverse!
print ret # => [[2, 1], [6, 5]]
print lists # => [[2, 1], [6, 5]]

one empty case

lists = [[1, 2], []]
ret = lists.together_reverse!
print ret # => [[2, 1], []]
print lists # => [[2, 1], []]


46
47
48
49
# File 'lib/open_classes/array/together_reverse.rb', line 46

def together_reverse!
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.reverse! }
end

#together_sample(count = nil) ⇒ Object Also known as: tsample

Arrays bulk sample.

together_sample has alias :tsample

not empty case

lists = [[1, 2], [5, 6]]
ret = lists.together_sample
print ret # => [1 or 2, 5 or 6]

empty case

lists = [[], []]
ret = lists.together_sample
print ret # => [nil, nil]

not empty case with args

lists = [[1, 2], [5, 6]]
ret = lists.together_sample 2
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]

not empty case with args

lists = [[], []]
ret = lists.together_sample 2
print ret # => [[], []]

not empty, over size case with args

lists = [[1, 2], [5, 6]]
ret = lists.together_sample 3
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]


39
40
41
42
43
44
45
46
# File 'lib/open_classes/array/together_sample.rb', line 39

def together_sample(count = nil)
  if_not_contain_array_rails_type_error
  if count.nil?
    reduce([]) { |ret, list|ret << list.sample }
  else
    reduce([]) { |ret, list|ret << list.sample(count) }
  end
end

#together_selectObject

Arrays loop together select.

together_select has aliases [:tselect, :together_find_all, :tfindall]

if you want to single Array return

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_select{|first, second|first == second}
print ret # => output  [[2, 3], [2, 3]]

if you want to multi Array return

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
print ret # => output  [[1, 3], [4, 2]]


25
26
27
28
29
30
31
32
33
34
# File 'lib/open_classes/array/together_select.rb', line 25

def together_select
  if_not_contain_array_rails_type_error
  ret = []
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i
    each_ret = instance_eval "yield(#{eval_each_str})"
    ret = set_together_each_return_select(ret, each_ret, i)
  end
  ret
end

#together_shift(count = nil) ⇒ Object Also known as: tshift

Arrays bulk shift.

together_shift has alias :tshift

not empty case

lists = [[1, 2], [5, 6]]
ret = lists.together_shift
print ret # => [1, 5]
print lists # => [2, 6]

empty case

lists = [[], []]
ret = lists.together_shift
print ret # => [nil, nil]
print lists # => [[], []]

not empty case with args

lists = [[1, 2], [5, 6]]
ret = lists.together_shift 2
print ret # => [[1, 2], [5, 6]]
print lists # => [[], []]

not empty case with args

lists = [[], []]
ret = lists.together_shift 2
print ret # => [[], []]
print lists # => [[], []]


37
38
39
40
41
42
43
44
# File 'lib/open_classes/array/together_shift.rb', line 37

def together_shift(count = nil)
  if_not_contain_array_rails_type_error
  if count.nil?
    reduce([]) { |ret, list|ret << list.shift }
  else
    reduce([]) { |ret, list|ret << list.shift(count) }
  end
end

#together_shuffle(count = nil) ⇒ Object Also known as: tshuffle

Arrays bulk shuffle.

together_shuffle has alias :tshuffle

lists = [[1, 2], [5, 6]]
ret = lists.together_shuffle
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]


17
18
19
20
# File 'lib/open_classes/array/together_shuffle.rb', line 17

def together_shuffle(count = nil)
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.shuffle }
end

#together_slice(*args) ⇒ Object Also known as: tslice

Arrays bulk slice.

together_slice has alias :tslice

Examples

single args case

lists = [[*1..5], [*6..10]]
ret = lists.together_slice 2
print ret # => [3, 8]

multi args case

lists = [[*1..5], [*6..10]]
ret = lists.together_slice 2, 2
print ret # => [[3, 4], [8, 9]]

range args case

lists = [[*1..5], [*6..10]]
ret = lists.together_slice (2..3)
print ret # => [[3, 4], [8, 9]]


32
33
34
35
# File 'lib/open_classes/array/together_slice.rb', line 32

def together_slice(*args)
  if_not_contain_array_rails_type_error
  reduce([]) { |ret, list|ret << list.slice(*args) }
end

#together_with_indexObject

Arrays loop together with index.

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_with_index do |first, second, index|
  print "#{index.to_s}:#{first}:#{second}\n"  # => output 0:one:1, 1:two:2, 2:three:3
end


17
18
19
20
21
22
23
# File 'lib/open_classes/array/together_with_index.rb', line 17

def together_with_index
  if_not_contain_array_rails_type_error
  first.each_with_index do |i_v, i|
    eval_each_str = get_args_str_for_together i, true
    instance_eval "yield(#{eval_each_str})"
  end
end

#uniq_sizeObject

return uniq size

Example

([*1..6] + [2,3]).uniq_size # => 6
[*1..6].uniq_size # => 6
[].uniq_size # => 0


13
14
15
# File 'lib/open_classes/array/uniq_size.rb', line 13

def uniq_size
  uniq.size
end