Class: Array
Overview
Array
Instance Method Summary collapse
-
#together ⇒ Object
Arrays loop together.
-
#together_map ⇒ Object
Arrays together map.
-
#together_reduce(init = nil) ⇒ Object
Arrays loop together reduce.
-
#together_select ⇒ Object
Arrays loop together select.
-
#together_with_index ⇒ Object
Arrays loop together with index.
Instance Method Details
#together ⇒ Object
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
14 15 16 17 18 19 20 |
# File 'lib/open_classes/array.rb', line 14 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_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}
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']]
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/open_classes/array.rb', line 54 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_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}
124 125 126 127 128 129 130 131 132 |
# File 'lib/open_classes/array.rb', line 124 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_select ⇒ Object
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]]
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/open_classes/array.rb', line 80 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_with_index ⇒ Object
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
29 30 31 32 33 34 35 |
# File 'lib/open_classes/array.rb', line 29 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 |