How to get the highest and the lowest points in the hash? - ruby

#participants[id] = {nick: nick, points: 1}
=> {"1"=>{:nick=>"Test", :points=>3}, "30"=>{:nick=>"AnotherTest", :points=>5}, "20"=>{:nick=>"Newtest", :points=>3}}
I want my the lowest points (ID: 1 and 20). How do I get the lowest points go first and then ID 30 go last?

If you use Enumerable#max_by() or Enumerable#min_by() you can do following;
data = {
"1" => {nick: "U1", points: 3},
"30" => {nick: "U30", points: 5},
"20" => {nick: "U20", points: 3}
}
max_id, max_data = data.max_by {|k,v| v[:points]}
puts max_id # => 30
puts max_data # => {nick: "U30", points: 5}
Same thing works with #min_by() and if you want to get back Hash you do this:
minimal = Hash[*data.min_by {|k,v| v[:points]}]
puts minimal # => {"1"=>{:nick=>"U1", :points=>3}}
Functions min_by() and max_by() will always return one record. If you want to get all records with same points then you have to use min / max data an do another "lookup" like this:
min_id, min_data = data.min_by {|k,v| v[:points]}
all_minimal = data.select {|k,v| v[:points] == min_data[:points]}
puts all_minimal
# => {"1"=>{:nick=>"U1", :points=>3}, "20"=>{:nick=>"U20", :points=>3}}

Hashes are not a suitable data structure for this operation. They are meant when you need to get a value in O(1) complexity.
You are better off using a sorted array or a tree if you are interested in comparisons or Heap (in case you are interested in only maximum or minimum value) as #Vadim suggested

Use Enumerable#minmax_by:
h = { "1"=>{:nick=>"Test", :points=>3},
"30"=>{:nick=>"AnotherTest", :points=>5},
"20"=>{:nick=>"Newtest", :points=>3}}
h.minmax_by { |_,g| g[:points] }
#=> [[ "1", {:nick=>"Test", :points=>3}],
# ["30", {:nick=>"AnotherTest", :points=>5}]]

You can work around the fact that min_by and max_by are returning only one result by combining sort and chunk:
data.sort_by{|_,v| v[:points]}.chunk{|(_,v)| v[:points]}.first.last.map(&:first)
#=> ["1", "20"]

Related

Convert a matrix into a hash data strucutre

i have a matrix like this
[
["name", "company1", "company2", "company3"],
["hr_admin", "Tom", "Joane", "Kris"],
["manager", "Philip", "Daemon", "Kristy"]
]
How can I convert into this data structure?
{
"company1" => {
"hr_admin"=> "Tom",
"manager" => "Philip"
},
"Company2" => {
"hr_admin"=> "Joane",
"manager" => "Daemon"
},
"company3" => {
"hr_admin"=> "Kris",
"manager" => "Kristy"
}
}
I have tried approach like taking out the first row of matrix (header) and zipping the rest o
f the matrix to change their position. It worked to some extent but it doesnt looks very good. So I am turning up here for help.
matrix[0][1...matrix[0].length].each_with_index.map do |x,i|
values = matrix[1..matrix.length].map do |x|
[x[0], x[i+1]]
end.to_h
[x, values]
end.to_h
matrix[0].length and matrix.length could be omittable depending on ruby version.
First you take all elements of first row but first.
then you map them with index to e.g. [["hr_admin", "Tom"],["manager", "Phil"]] using the index
then you call to_h on every element and on whole array.
arr = [
["name", "company1", "company2", "company3"],
["hr_admin", "Tom", "Joane", "Kris"],
["manager", "Philip", "Daemon", "Kristy"]
]
Each key-value pair of the hash to be constructed is formed from the "columns" of arr. It therefore is convenient to compute the transpose of arr:
(_, *positions), *by_company = arr.transpose
#=> [["name", "hr_admin", "manager"],
# ["company1", "Tom", "Philip"],
# ["company2", "Joane", "Daemon"],
# ["company3", "Kris", "Kristy"]]
I made use of Ruby's array decomposition (a.k.a array destructuring) feature (see this blog for elabortion) to assign different parts of the inverse of arr to variables. Those values are as follows1.
_ #=> "name"
positions
#=> ["hr_admin", "manager"]
by_company
#=> [["company1", "Tom", "Philip"],
# ["company2", "Joane", "Daemon"],
# ["company3", "Kris", "Kristy"]]
It is now a simple matter to form the desired hash. Once again I will use array decomposition to advantage.
by_company.each_with_object({}) do |(company_name, *employees),h|
h[company_name] = positions.zip(employees).to_h
end
#=> {"company1"=>{"hr_admin"=>"Tom", "manager"=>"Philip"},
# "company2"=>{"hr_admin"=>"Joane", "manager"=>"Daemon"},
# "company3"=>{"hr_admin"=>"Kris", "manager"=>"Kristy"}}
When, for example,
company_name, *employees = ["company1", "Tom", "Philip"]
company_name
#=> "company1"
employees
#=> ["Tom", "Philip"]
so
h[company_name] = positions.zip(employees).to_h
h["company1"] = ["hr_admin", "manager"].zip(["Tom", "Philip"]).to_h
= [["hr_admin", "Tom"], ["manager", "Philip"]].to_h
= {"hr_admin"=>"Tom", "manager"=>"Philip"}
Note that these calculations do not depend on the numbers of rows or columns of arr.
1. As is common practice, I used the special variable _ to signal to the reader that its value is not used in subsequent calculations.

How to sort an array of hashes with multiple values?

I have an array of hashes which contains an id field and a weight field. I am able to sort it based on weight but I also need to make sure that the id field is sorted if there are duplicate weights. Below is the code snippet for reference.
# Input
arr = [{"id" => 10, "weight" => 23}, {"id" => 6, "weight" => 43}, {"id" => 12, "weight" => 5}, {"id" => 15, "weight" => 30}, {"id" => 11, "weight" => 5}]
arr.sort_by{|k| k["weight"]}
# Output: [{"id"=>12, "weight"=>5}, {"id"=>11, "weight"=>5}, {"id"=>10, "weight"=>23}, {"id"=>15, "weight"=>30}, {"id"=>6, "weight"=>43}]
# Expected output = [{"id"=>11, "weight"=>5}, {"id"=>12, "weight"=>5}, {"id"=>10, "weight"=>23}, {"id"=>15, "weight"=>30}, {"id"=>6, "weight"=>43}]
In the above example, id = 12 and id = 11 have the duplicate values. I need to have id = 11 before id = 12 in the array. I really appreciate some guidance on this. Thank you!
You can use Enumerable#sort_by with an array too. Note the order of the values in the array.
arr.sort_by {|k| k.values_at("weight", "id") }
Quote from the docs of Array#<=> about how comparison of array works:
Arrays are compared in an “element-wise” manner; the first element of ary is compared with the first one of other_ary using the <=> operator, then each of the second elements, etc… As soon as the result of any such comparison is non zero (i.e. the two corresponding elements are not equal), that result is returned for the whole array comparison.
You can use Array#sort with the spaceship operator like so:
arr.sort do |a,b|
if a["weight"] == b["weight"]
a["id"] <=> b["id"]
else
a["weight"] <=> b["weight"]
end
end

Sorting specific objects in an array

I'm new to Ruby and looking to sort only certain items in my collection.
For example if I have the following array. I only want to sort the objects that contains the property type: 'sort'
object = [{
type: 'sort',
id: 3
}, {
type: 'notsort',
id: 4
}, {
type: 'sort',
id: 1
}, {
type: 'sort',
id: 0
}
]
I need the order to map directly to the id map below.
sortIdOrder = [0, 1, 3]
The end result should look like:
object = [{
type: 'notsort',
id: 4
}, {
type: 'sort',
id: 0
},{
type: 'sort',
id: 1
}, {
type: 'sort',
id: 3
}]
As you can see the array is sorted by id based on the sortIdOrder . The notsort type can either be at the end or start.
Sorting can be expensive, so one should not sort when the desired order is known, as it is here.
I've assumed that the values :id are unique, as the question would not make sense if they were not.
First partition the hashes into those to be sorted and the rest.
sortees, nonsortees = object.partition { |h| h[:type] == 'sort' }
#=> [[{:type=>"sort", :id=>3}, {:type=>"sort", :id=>1}, {:type=>"sort", :id=>0}],
# [{:type=>"notsort", :id=>4}]]
so
sortees
#=> [{:type=>"sort", :id=>3}, {:type=>"sort", :id=>1}, {:type=>"sort", :id=>0}]
nonsortees
#=> [{:type=>"notsort", :id=>4}]
I'll put the elements of sortees in the desired order then concatenate that array with nonsortees, putting the hashes that are not to be sorted at the end.
I order the elements of sortees by creating a hash with one key-value pair g[:id]=>g for each element g (a hash) of sortees. That allows me to use Hash#values_at to pull out the desired hashes in the specified order.
sortees.each_with_object({}) { |g,h| h[g[:id]] = g }.
values_at(*sortIdOrder).
concat(nonsortees)
#=> [{:type=>"sort", :id=>0}, {:type=>"sort", :id=>1}, {:type=>"sort", :id=>3},
# {:type=>"notsort", :id=>4}]
Note that
sortees.each_with_object({}) { |g,h| h[g[:id]] = g }
#=> {3=>{:type=>"sort", :id=>3}, 1=>{:type=>"sort", :id=>1},
# 0=>{:type=>"sort", :id=>0}}
A not very performant one-liner:
object.sort_by{|o| sortIdOrder.index(o[:id]) || -1}
This makes the notsort objects appear at the head of sorted array. It's an O(m * nlog(n)) algorithm, where n is the size of object and m is the size of sortIdOrder. This is faster when your object and sortIdOrder are small.
A more performant one for large arrays is
order = sortIdOrder.each.with_index.with_object(Hash.new(-1)) {|(id, index), h| h[id] = index}
object.sort_by{|o| order[o[:id]]}
This is an O(m + nlog(n)) algorithm but requires more memory.
you can use sort with a block that sorts by :type, then :id.
object.sort {|a, b| [a[:type], a[:id]] <=> [b[:type], b[:id]] }
[{:type=>"notsort", :id=>4},
{:type=>"sort", :id=>0},
{:type=>"sort", :id=>1},
{:type=>"sort", :id=>3}]
I'd go with something like this:
object.sort_by do |o|
[
(o[:type] == :sort) ? 0 : 1,
sortIdOrder.index(o[:id])
]
end
When sorting by an array, you're essentially sorting by the first element, except when they're the same, in which case you sort by the second element, etc. In code above, (o[:type] == :sort) ? 0 : 1 ensures that everything with a type of :sort comes first, and everything else after, even if the type is nil, or 5 or whatever you like. The sortIdOrder.index(o[:id]) term ensures things are sorted as you like (though items with no :id, or whose :id is not found in sortIdOrder will be ordered arbitrarily. If your data set is very large, you may want to tweak this further so that the sortIdOrder array is not performed for non-sort items.
Enumerable#sort_by only has to call the block once for each element, and then perform fast comparisons on the results; Enumerable#sort has to call the block on pairs of elements, which means it's called more often:
irb(main):015:0> ary = %w{9 8 7 6 5 4 3 2 1}
=> ["9", "8", "7", "6", "5", "4", "3", "2", "1"]
irb(main):016:0> a = 0; ary.sort_by {|x| puts x; a+=1; x.to_i }; puts "Total: #{a}"
9
8
7
6
5
4
3
2
1
Total: 9
=> nil
irb(main):017:0> a = 0; ary.sort {|x,y| puts "#{x},#{y}"; a+=1; x.to_i <=> y.to_i }; puts "Total: #{a}"
9,5
5,1
8,5
2,5
7,5
3,5
6,5
4,5
6,8
8,9
7,8
6,7
1,3
3,4
2,3
1,2
Total: 16
=> nil
In these cases, it's really not very important, because hash access is fast anyway (though sort_by is still more legible), but in cases where calculating the attribute you want to sort by is even moderately expensive, sort_by can be quite a bit faster. The block form of sort is mostly useful if the comparison logic itself is complicated.
Maybe I'm late, but my solution is:
Rails Solution:
object.partition { |hash| hash[:id].in?(sortIdOrder) }.flatten.reverse
Ruby Solution:
object.partition { |hash| sortIdOrder.include? hash[:id] }.flatten.reverse
both of it in result give this:
=> [{:type=>"notsort", :id=>4},
{:type=>"sort", :id=>0},
{:type=>"sort", :id=>1},
{:type=>"sort", :id=>3}]

Finding N keys with highest value in hash, keeping order

In a Ruby script,
I have a hash that has sentences as keys and relevance scores as values.
I want to retrieve an array containing the N most relevant sentences (highest scores).
I want to retain the order in which these sentences are extracted.
Given:
hash = {
'This is the first sentence.' => 5,
'This is the second sentence.' => 1,
'This is the last sentence.' => 6
}
Then:
choose_best(hash, 2)
Should return:
['This is the first sentence.', 'This is the last sentence.']
All the methods I can think of involve reordering the hash, thus losing the order of the sentences. What would be the best way to tackle this?
Try the following monster:
hash.map(&:reverse).each_with_index
.sort_by(&:first).reverse
.take(2)
.sort_by(&:last)
.map { |(_,s),_| s }
Another functional one:
hash.to_a.values_at(*hash.values.each_with_index
.sort.reverse
.map(&:last)
.sort.take(2))
.map(&:first)
Note however, that as an unordered data structure, a hash table is not really suitable for this use case (although the order is remembered in Ruby 1.9). You should use an array instead (the sorting code remains the same):
sentences = [
['This is the first sentence.', 5],
['This is the second sentence.', 1],
['This is the last sentence.', 6],
]
def extract hash, n
min = hash.values.sort[-n]
a = []
i = 0
hash.each{|k, v| (a.push(k) and i += 1) if i < n and v >= min}
a
end
hash = {
'This is the first sentence.' => 5,
'This is the second sentence.' => 1,
'This is the last sentence.' => 6
}
cutoff_val = hash.values.sort[-2] #cf. sawa
p hash.select{|k,v| v >= cutoff_val }
# =>{"This is the first sentence."=>5, "This is the last sentence."=>6}
Starting in Ruby 2.2.0, Enumerable#max_by takes an optional integer argument that makes it return an array instead of just a single element. Therefore, we can do:
hash = {
'This is the first sentence.' => 6,
'This is the second sentence.' => 1,
'This is the last sentence.' => 5
}
p hash.max_by(2, &:last).map(&:first).sort_by { |k| hash.keys.index k }
# => ["This is the first sentence.", "This is the last sentence."]
The call to sort_by at the end guarantees the sentences are in the right order, as you requested.
a = hash.sort_by { |sentence, score| score }.reverse
The array a now contains pairs of values of your top scoring sentences. You can select the first N of them.
hash = {"foo" => 7, "bar" => 2, "blah" => 3 }
a = hash.sort_by { |sentence, score| score }.reverse
=> [["foo", 7], ["blah", 3], ["bar", 2]]

What is the best way to convert an array to a hash in Ruby

In Ruby, given an array in one of the following forms...
[apple, 1, banana, 2]
[[apple, 1], [banana, 2]]
...what is the best way to convert this into a hash in the form of...
{apple => 1, banana => 2}
Simply use Hash[*array_variable.flatten]
For example:
a1 = ['apple', 1, 'banana', 2]
h1 = Hash[*a1.flatten(1)]
puts "h1: #{h1.inspect}"
a2 = [['apple', 1], ['banana', 2]]
h2 = Hash[*a2.flatten(1)]
puts "h2: #{h2.inspect}"
Using Array#flatten(1) limits the recursion so Array keys and values work as expected.
NOTE: For a concise and efficient solution, please see Marc-André Lafortune's answer below.
This answer was originally offered as an alternative to approaches using flatten, which were the most highly upvoted at the time of writing. I should have clarified that I didn't intend to present this example as a best practice or an efficient approach. Original answer follows.
Warning! Solutions using flatten will not preserve Array keys or values!
Building on #John Topley's popular answer, let's try:
a3 = [ ['apple', 1], ['banana', 2], [['orange','seedless'], 3] ]
h3 = Hash[*a3.flatten]
This throws an error:
ArgumentError: odd number of arguments for Hash
from (irb):10:in `[]'
from (irb):10
The constructor was expecting an Array of even length (e.g. ['k1','v1,'k2','v2']). What's worse is that a different Array which flattened to an even length would just silently give us a Hash with incorrect values.
If you want to use Array keys or values, you can use map:
h3 = Hash[a3.map {|key, value| [key, value]}]
puts "h3: #{h3.inspect}"
This preserves the Array key:
h3: {["orange", "seedless"]=>3, "apple"=>1, "banana"=>2}
The best way is to use Array#to_h:
[ [:apple,1],[:banana,2] ].to_h #=> {apple: 1, banana: 2}
Note that to_h also accepts a block:
[:apple, :banana].to_h { |fruit| [fruit, "I like #{fruit}s"] }
# => {apple: "I like apples", banana: "I like bananas"}
Note: to_h accepts a block in Ruby 2.6.0+; for early rubies you can use my backports gem and require 'backports/2.6.0/enumerable/to_h'
to_h without a block was introduced in Ruby 2.1.0.
Before Ruby 2.1, one could use the less legible Hash[]:
array = [ [:apple,1],[:banana,2] ]
Hash[ array ] #= > {:apple => 1, :banana => 2}
Finally, be wary of any solutions using flatten, this could create problems with values that are arrays themselves.
Update
Ruby 2.1.0 is released today. And I comes with Array#to_h (release notes and ruby-doc), which solves the issue of converting an Array to a Hash.
Ruby docs example:
[[:foo, :bar], [1, 2]].to_h # => {:foo => :bar, 1 => 2}
Edit: Saw the responses posted while I was writing, Hash[a.flatten] seems the way to go.
Must have missed that bit in the documentation when I was thinking through the response. Thought the solutions that I've written can be used as alternatives if required.
The second form is simpler:
a = [[:apple, 1], [:banana, 2]]
h = a.inject({}) { |r, i| r[i.first] = i.last; r }
a = array, h = hash, r = return-value hash (the one we accumulate in), i = item in the array
The neatest way that I can think of doing the first form is something like this:
a = [:apple, 1, :banana, 2]
h = {}
a.each_slice(2) { |i| h[i.first] = i.last }
You can also simply convert a 2D array into hash using:
1.9.3p362 :005 > a= [[1,2],[3,4]]
=> [[1, 2], [3, 4]]
1.9.3p362 :006 > h = Hash[a]
=> {1=>2, 3=>4}
Summary & TL;DR:
This answer hopes to be a comprehensive wrap-up of information from other answers.
The very short version, given the data from the question plus a couple extras:
flat_array = [ apple, 1, banana, 2 ] # count=4
nested_array = [ [apple, 1], [banana, 2] ] # count=2 of count=2 k,v arrays
incomplete_f = [ apple, 1, banana ] # count=3 - missing last value
incomplete_n = [ [apple, 1], [banana ] ] # count=2 of either k or k,v arrays
# there's one option for flat_array:
h1 = Hash[*flat_array] # => {apple=>1, banana=>2}
# two options for nested_array:
h2a = nested_array.to_h # since ruby 2.1.0 => {apple=>1, banana=>2}
h2b = Hash[nested_array] # => {apple=>1, banana=>2}
# ok if *only* the last value is missing:
h3 = Hash[incomplete_f.each_slice(2).to_a] # => {apple=>1, banana=>nil}
# always ok for k without v in nested array:
h4 = Hash[incomplete_n] # or .to_h => {apple=>1, banana=>nil}
# as one might expect:
h1 == h2a # => true
h1 == h2b # => true
h1 == h3 # => false
h3 == h4 # => true
Discussion and details follow.
Setup: variables
In order to show the data we'll be using up front, I'll create some variables to represent various possibilities for the data. They fit into the following categories:
Based on what was directly in the question, as a1 and a2:
(Note: I presume that apple and banana were meant to represent variables. As others have done, I'll be using strings from here on so that input and results can match.)
a1 = [ 'apple', 1 , 'banana', 2 ] # flat input
a2 = [ ['apple', 1], ['banana', 2] ] # key/value paired input
Multi-value keys and/or values, as a3:
In some other answers, another possibility was presented (which I expand on here) – keys and/or values may be arrays on their own:
a3 = [ [ 'apple', 1 ],
[ 'banana', 2 ],
[ ['orange','seedless'], 3 ],
[ 'pear', [4, 5] ],
]
Unbalanced array, as a4:
For good measure, I thought I'd add one for a case where we might have an incomplete input:
a4 = [ [ 'apple', 1],
[ 'banana', 2],
[ ['orange','seedless'], 3],
[ 'durian' ], # a spiky fruit pricks us: no value!
]
Now, to work:
Starting with an initially-flat array, a1:
Some have suggested using #to_h (which showed up in Ruby 2.1.0, and can be backported to earlier versions). For an initially-flat array, this doesn't work:
a1.to_h # => TypeError: wrong element type String at 0 (expected array)
Using Hash::[] combined with the splat operator does:
Hash[*a1] # => {"apple"=>1, "banana"=>2}
So that's the solution for the simple case represented by a1.
With an array of key/value pair arrays, a2:
With an array of [key,value] type arrays, there are two ways to go.
First, Hash::[] still works (as it did with *a1):
Hash[a2] # => {"apple"=>1, "banana"=>2}
And then also #to_h works now:
a2.to_h # => {"apple"=>1, "banana"=>2}
So, two easy answers for the simple nested array case.
This remains true even with sub-arrays as keys or values, as with a3:
Hash[a3] # => {"apple"=>1, "banana"=>2, ["orange", "seedless"]=>3, "pear"=>[4, 5]}
a3.to_h # => {"apple"=>1, "banana"=>2, ["orange", "seedless"]=>3, "pear"=>[4, 5]}
But durians have spikes (anomalous structures give problems):
If we've gotten input data that's not balanced, we'll run into problems with #to_h:
a4.to_h # => ArgumentError: wrong array length at 3 (expected 2, was 1)
But Hash::[] still works, just setting nil as the value for durian (and any other array element in a4 that's just a 1-value array):
Hash[a4] # => {"apple"=>1, "banana"=>2, ["orange", "seedless"]=>3, "durian"=>nil}
Flattening - using new variables a5 and a6
A few other answers mentioned flatten, with or without a 1 argument, so let's create some new variables:
a5 = a4.flatten
# => ["apple", 1, "banana", 2, "orange", "seedless" , 3, "durian"]
a6 = a4.flatten(1)
# => ["apple", 1, "banana", 2, ["orange", "seedless"], 3, "durian"]
I chose to use a4 as the base data because of the balance problem we had, which showed up with a4.to_h. I figure calling flatten might be one approach someone might use to try to solve that, which might look like the following.
flatten without arguments (a5):
Hash[*a5] # => {"apple"=>1, "banana"=>2, "orange"=>"seedless", 3=>"durian"}
# (This is the same as calling `Hash[*a4.flatten]`.)
At a naïve glance, this appears to work – but it got us off on the wrong foot with the seedless oranges, thus also making 3 a key and durian a value.
And this, as with a1, just doesn't work:
a5.to_h # => TypeError: wrong element type String at 0 (expected array)
So a4.flatten isn't useful to us, we'd just want to use Hash[a4]
The flatten(1) case (a6):
But what about only partially flattening? It's worth noting that calling Hash::[] using splat on the partially-flattened array (a6) is not the same as calling Hash[a4]:
Hash[*a6] # => ArgumentError: odd number of arguments for Hash
Pre-flattened array, still nested (alternate way of getting a6):
But what if this was how we'd gotten the array in the first place?
(That is, comparably to a1, it was our input data - just this time some of the data can be arrays or other objects.) We've seen that Hash[*a6] doesn't work, but what if we still wanted to get the behavior where the last element (important! see below) acted as a key for a nil value?
In such a situation, there's still a way to do this, using Enumerable#each_slice to get ourselves back to key/value pairs as elements in the outer array:
a7 = a6.each_slice(2).to_a
# => [["apple", 1], ["banana", 2], [["orange", "seedless"], 3], ["durian"]]
Note that this ends up getting us a new array that isn't "identical" to a4, but does have the same values:
a4.equal?(a7) # => false
a4 == a7 # => true
And thus we can again use Hash::[]:
Hash[a7] # => {"apple"=>1, "banana"=>2, ["orange", "seedless"]=>3, "durian"=>nil}
# or Hash[a6.each_slice(2).to_a]
But there's a problem!
It's important to note that the each_slice(2) solution only gets things back to sanity if the last key was the one missing a value. If we later added an extra key/value pair:
a4_plus = a4.dup # just to have a new-but-related variable name
a4_plus.push(['lychee', 4])
# => [["apple", 1],
# ["banana", 2],
# [["orange", "seedless"], 3], # multi-value key
# ["durian"], # missing value
# ["lychee", 4]] # new well-formed item
a6_plus = a4_plus.flatten(1)
# => ["apple", 1, "banana", 2, ["orange", "seedless"], 3, "durian", "lychee", 4]
a7_plus = a6_plus.each_slice(2).to_a
# => [["apple", 1],
# ["banana", 2],
# [["orange", "seedless"], 3], # so far so good
# ["durian", "lychee"], # oops! key became value!
# [4]] # and we still have a key without a value
a4_plus == a7_plus # => false, unlike a4 == a7
And the two hashes we'd get from this are different in important ways:
ap Hash[a4_plus] # prints:
{
"apple" => 1,
"banana" => 2,
[ "orange", "seedless" ] => 3,
"durian" => nil, # correct
"lychee" => 4 # correct
}
ap Hash[a7_plus] # prints:
{
"apple" => 1,
"banana" => 2,
[ "orange", "seedless" ] => 3,
"durian" => "lychee", # incorrect
4 => nil # incorrect
}
(Note: I'm using awesome_print's ap just to make it easier to show the structure here; there's no conceptual requirement for this.)
So the each_slice solution to an unbalanced flat input only works if the unbalanced bit is at the very end.
Take-aways:
Whenever possible, set up input to these things as [key, value] pairs (a sub-array for each item in the outer array).
When you can indeed do that, either #to_h or Hash::[] will both work.
If you're unable to, Hash::[] combined with the splat (*) will work, so long as inputs are balanced.
With an unbalanced and flat array as input, the only way this will work at all reasonably is if the last value item is the only one that's missing.
Side-note: I'm posting this answer because I feel there's value to be added – some of the existing answers have incorrect information, and none (that I read) gave as complete an answer as I'm endeavoring to do here. I hope that it's helpful. I nevertheless give thanks to those who came before me, several of whom provided inspiration for portions of this answer.
Appending to the answer but using anonymous arrays and annotating:
Hash[*("a,b,c,d".split(',').zip([1,2,3,4]).flatten)]
Taking that answer apart, starting from the inside:
"a,b,c,d" is actually a string.
split on commas into an array.
zip that together with the following array.
[1,2,3,4] is an actual array.
The intermediate result is:
[[a,1],[b,2],[c,3],[d,4]]
flatten then transforms that to:
["a",1,"b",2,"c",3,"d",4]
and then:
*["a",1,"b",2,"c",3,"d",4] unrolls that into
"a",1,"b",2,"c",3,"d",4
which we can use as the arguments to the Hash[] method:
Hash[*("a,b,c,d".split(',').zip([1,2,3,4]).flatten)]
which yields:
{"a"=>1, "b"=>2, "c"=>3, "d"=>4}
if you have array that looks like this -
data = [["foo",1,2,3,4],["bar",1,2],["foobar",1,"*",3,5,:foo]]
and you want the first elements of each array to become the keys for the hash and the rest of the elements becoming value arrays, then you can do something like this -
data_hash = Hash[data.map { |key| [key.shift, key] }]
#=>{"foo"=>[1, 2, 3, 4], "bar"=>[1, 2], "foobar"=>[1, "*", 3, 5, :foo]}
Not sure if it's the best way, but this works:
a = ["apple", 1, "banana", 2]
m1 = {}
for x in (a.length / 2).times
m1[a[x*2]] = a[x*2 + 1]
end
b = [["apple", 1], ["banana", 2]]
m2 = {}
for x,y in b
m2[x] = y
end
For performance and memory allocation concerns please check my answer to Rails mapping array of hashes onto single hash where I bench-marked several solutions.
reduce / inject can be the fastest or the slowest solution depending on which method you use it which.
If the numeric values are seq indexes, then we could have simpler ways...
Here's my code submission, My Ruby is a bit rusty
input = ["cat", 1, "dog", 2, "wombat", 3]
hash = Hash.new
input.each_with_index {|item, index|
if (index%2 == 0) hash[item] = input[index+1]
}
hash #=> {"cat"=>1, "wombat"=>3, "dog"=>2}

Resources