Ruby: How to concatenate array of arrays into one
You're looking for #flatten
:
concatenated = array_of_arrays.flatten
By default, this will flatten the lists recursively. #flatten
accepts an optional argument to limit the recursion depth – the documentation lists examples to illustrate the difference.
Merge arrays in Ruby/Rails
Like this?
⚡️ irb
2.2.2 :001 > [1,2,3] + [4,5,6]
=> [1, 2, 3, 4, 5, 6]
But you don't have 2 arrays.
You could do something like:
@movie = Movie.first()
@options = Movie.order("RANDOM()").first(3).to_a << @movie
How to efficiently concatenate multiple arrays in Ruby?
If you've already determined that multiple concatenation is the fastest method, you can write it nicer using reduce:
[foo, bar, baz].reduce([], :concat)
Ruby group array of arrays to merge them into one single array based on unique key in arrays
I'm not certain that I understand the question but I expect you may be looking for the following.
arr = [
["ABC", "5A2", nil, "88474"],
["ABC", nil, "2", "88474"],
["ABC", nil, nil, "88474"],
["ABC", nil, nil, "88474"],
["Jack", "5A2", nil, "05195"],
["Jack", nil, "2", "05195"],
["Jack", nil, nil, "05195"],
["Jack", nil, nil, "05195"]
]
arr.each_with_object({}) do |a, h|
h.update(a.first=>a) { |_k, oa, na| oa.zip(na).map { |ov, nv| ov.nil? ? nv : ov } }
end.values
#=> [["ABC", "5A2", "2", "88474"], ["Jack", "5A2", "2", "05195"]]
This uses the form of Hash#update (a.k.a. merge!
) that employs the block
{ |_k, oa, na| oa.zip(na).map { |ov, nv| ov.nil? ? nv : ov } }
to determine the values of keys that are present in both the hash being built (h
) and the hash being merged ({ a.first=>a }
). See the doc for a description of the three block variables, _k
, oa
and na
.1
I can best explain how the calculations procede by salting the code with puts
statements and running it with an abbreviated array arr
.
arr = [
["ABC", "5A2", nil, "88474"],
["ABC", nil, "2", "88474"],
["Jack", "5A2", nil, "05195"],
["Jack", nil, "2", "05195"],
]
arr.each_with_object({}) do |a, h|
puts "\na = #{a}"
puts "h = #{h}"
puts "a.first=>a = #{a.first}=>#{a}"
h.update(a.first=>a) do |_k, oa, na|
puts "_k = #{_k}"
puts "oa = #{oa}"
puts "na = #{na}"
a = oa.zip(na)
puts "oa.zip(na) = #{a}"
a.map do |ov, nv|
puts " ov = #{ov}, nv = #{nv}"
puts " ov.nil? ? nv : ov = #{ov.nil? ? nv : ov}"
ov.nil? ? nv : ov
end
end
end.tap { |h| puts "h = #{h}" }.values
#=> [["ABC", "5A2", "2", "88474"], ["Jack", "5A2", "2", "05195"]]
The following is displayed.
a = ["ABC", "5A2", nil, "88474"]
h = {}
a.first=>a = ABC=>["ABC", "5A2", nil, "88474"]
(The block is not called here because h does not have a key "ABC")
a = ["ABC", nil, "2", "88474"]
h = {"ABC"=>["ABC", "5A2", nil, "88474"]}
a.first=>a = ABC=>["ABC", nil, "2", "88474"]
_k = ABC
oa = ["ABC", "5A2", nil, "88474"]
na = ["ABC", nil, "2", "88474"]
oa.zip(na) = [["ABC", "ABC"], ["5A2", nil], [nil, "2"], ["88474", "88474"]]
ov = ABC, nv = ABC
ov.nil? ? nv : ov = ABC
ov = 5A2, nv =
ov.nil? ? nv : ov = 5A2
ov = , nv = 2
ov.nil? ? nv : ov = 2
ov = 88474, nv = 88474
ov.nil? ? nv : ov = 88474
a = ["Jack", "5A2", nil, "05195"]
h = {"ABC"=>["ABC", "5A2", "2", "88474"]}
a.first=>a = Jack=>["Jack", "5A2", nil, "05195"]
(The block is not called here because h does not have a key "Jack")
a = ["Jack", nil, "2", "05195"]
h = {"ABC"=>["ABC", "5A2", "2", "88474"], "Jack"=>["Jack", "5A2", nil, "05195"]}
a.first=>a = Jack=>["Jack", nil, "2", "05195"]
_k = Jack
oa = ["Jack", "5A2", nil, "05195"]
na = ["Jack", nil, "2", "05195"]
oa.zip(na) = [["Jack", "Jack"], ["5A2", nil], [nil, "2"], ["05195", "05195"]]
ov = Jack, nv = Jack
ov.nil? ? nv : ov = Jack
ov = 5A2, nv =
ov.nil? ? nv : ov = 5A2
ov = , nv = 2
ov.nil? ? nv : ov = 2
ov = 05195, nv = 05195
ov.nil? ? nv : ov = 05195
h = {"ABC"=>["ABC", "5A2", "2", "88474"], "Jack"=>["Jack", "5A2", "2", "05195"]}
1. As is common practice, I began the name of the common key, _k
, with an underscore to signal to the reader that it is not used in the block calculation. Often you will see such a block variable represented by an underscore alone.
How to merge an array of arrays together into a single array?
[["convertible", "2010", "red"], ["convertible", "2010", "green"]].flatten!
Array of arrays merge in Rails
You could use Hash#merge! method with a block for that:
initial = [['Area1', 12345], ['Area2', 54321]].to_h
initial.merge!({'Area1' => 1212 }) { |_,v1,v2| [v1,v2].flatten }
#=> {"Area1"=>[12345, 1212], "Area2"=>54321}
and if you want the result to be an array, you can use
initial.merge!({'Area1' => 1212 }) { |_,v1,v2| [v1,v2].flatten }.to_a
Combine array of array into all possible combinations, forward only, in Ruby
Know your Array#product
:
a = [['1','2'],['a','b'],['x','y']]
a.first.product(*a[1..-1]).map(&:join)
Related Topics
Sidekiq Configuration for Multiple Environments
How to Make Nokogiri Not to Convert &Nbsp; to Space
Rails 4.0.1 on Heroku, Can't Create Database
Can't Install Ruby-Debug-Base19X Gem
Change Rails Version Used by Rvm
Ruby: Is There an Opposite of Include? for Ruby Arrays
Solutions to the Annoying "Warning: Already Initialized Constant" Message
Ruby: Most Concise Way to Use an Env Variable If It Exists, Otherwise Use Default Value
Cannot Start Rails 4 Console on Production Server
Passing Error Messages Through Flash
What Is Catch and Throw Used for in Ruby
How to Manage Ruby Threads So They Finish All Their Work
Is Ruby on Rails (Or at Least the Community) Dying
Generate Unique Random String with Letters and Numbers in Lower Case