What is the best way to remap a Hash in Ruby? - ruby

Is there a simple way of remapping a hash in ruby the following way:
from:
{:name => "foo", :value => "bar"}
to:
{"foo" => "bar"}
Preferably in a way that makes it simple to do this operation while iterating over an array of this type of hashes:
from:
[{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]
to:
{"foo" => "bar", "foo2" => "bar2"}
Thanks.

arr = [ {:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]
result = {}
arr.each{|x| result[x[:name]] = x[:value]}
# result is now {"foo2"=>"bar2", "foo"=>"bar"}

A modified version of Vanson Samuel's code does the intended.
It's a one-liner, but quite a long one.
arr = [{:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]
arr.inject({}){|r,item| r.merge({item['name'] => item['value']})}
# result {"foo" => "bar", "foo2" => "bar2"}
I wouldn't say that it's prettier than Gishu's version, though.

As a general rule of thumb, if you have a hash of the form {:name => "foo", :value => "bar"}, you're usually better off with using a tuple of ["foo", "bar"].
arr = [["foo", "bar"], ["foo2", "bar2"]]
arr.inject({}) { |accu, (key, value)| accu[key] = value; accu }

I know this is old, but the neatest way to achieve this is to map the array of hashes to an array of tuples, then use Hash[] to build a hash from that, as follows:
arr = [{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]
Hash[ array.map { |item| [ item[:name], item[:value] ] } ]
# => {"foo"=>"bar", "foo2"=>"bar2"}

a bit late but:
[{ name: "foo", value: "bar" },{name: "foo2", value: "bar2"}].map{ |k| k.values }.to_h

Related

Merging two arrays of hashes in Ruby

I’m very close to solving a project I’ve been working on for a while but can’t seem to figure this out.
array1 = [{:new_listing => "1", :item => "apple"}, {:new_listing => "2", :item => "bannana"}]
array2 = [{:height => "10"}, {:height => "12"}]
How do I merge them so it is
[{:new_listing => "1", :item => "apple", :height => "10" },
{:new_listing => "2", :item => "bannana", :height => "12"}]
The order of each arrays are aligned, and should be the same size. Some values of array2 will be {:height => nil}.
The order of each arrays are aligned, and should be the same size.
That's exactly the description of the zip method.
zip(arg, ...) → an_array_of_array
Takes one element from enum and merges corresponding elements from each args.
Likewise, merging hashes is done with merge
array1.zip(array2).map { |x, y| x.merge(y) }
array1.map {|x| x.merge!(array2.shift) }
use map/each as per your requirement
Below solution should work for you:
array1 = [{:new_listing=> "1", :item=> "apple"}, {:new_listing=> "2", :item=> "bannana"}]
array2 = [{:height=> "10"},{:height => "12"}]
array2.each_with_index { |hash, index| array1[index] = array1[index].merge(hash) }
puts array1
# [{:new_listing=>"1", :item=>"apple", :height=>"10"}, {:new_listing=>"2", :item=>"bannana", :height=>"12"}]

How can I achieve certain values of a hash?

In the following nested hash,
hash = {a: 2, b: 4, c: {name: "John", id: 12, age: 33}}
I want to return the values that are hash:
{:name => "John", :id => 12, :age => 33}
I want to returned a hash. I thought the following code will do the job:
hash.select! {|_k, v| v.is_a?(Hash)}
# => {:c => {:name => "John", :id => 12, :age => 33}}
but I get both k/v pairs. Did I miss anything on the code? How can I achieve the return value as mentioned?
I would do something like:
hash.values.find(&Hash.method(:===))
#=> {:name=>"John", :id=>12, :age=>33}
select returns the key and value that matched. Add .values to get just the values without the keys:
hash.select! { |_k, v| v.is_a?(Hash) }.values
This will return an array of the values that were matched by select:
[{:name=>"John", :id=>12, :age=>33}]
If you know there will only ever be one result, you can get the desired value by calling first:
hash.select! { |_k, v| v.is_a?(Hash) }.values.first
{:name=>"John", :id=>12, :age=>33}

visiting hash with keys from array

I have a big hash with lots of nested key value pairs.
Eg.
h = {"foo" => {"bar" => {"hello" => {"world" => "result" } } } }
Now I want to access result and I have keys for that in array in proper sequence.
keys_arr = ["foo", "bar", "hello", "world"]
The motive is clear, I want to do following:
h["foo"]["bar"]["hello"]["world"]
# => "result"
But I don't know how to do this. I am currently doing:
key = '["' + keys_arr.join('"]["') + '"]'
eval("h"+key)
# => "result"
Which looks like a hack. Also it greatly reduces my ability to work with hash in real environment.
Please suggest alternate and better ways.
Using Enumerable#inject (or Enumerable#reduce):
h = {"foo" => {"bar" => {"hello" => {"world" => "result" } } } }
keys_arr = ["foo", "bar", "hello", "world"]
keys_arr.inject(h) { |x, k| x[k] }
# => "result"
UPDATE
If you want to do something like: h["foo"]["bar"]["hello"]["world"] = "ruby"
innermost = keys_arr[0...-1].inject(h) { |x, k| x[k] } # the innermost hash
innermost[keys_arr[-1]] = "ruby"
keys_arr.inject(h, :[])
will do
Another way:
h = {"foo" => {"bar" => {"hello" => {"world" => 10 } } } }
keys = ["foo", "bar", "hello", "world"]
result = h
keys.each do |key|
result = result[key]
end
puts result #=>10
If the key may not exist, see here:
Dealing with many [...] in Ruby

Is there a way to a regex query on a json structure's keys?

Say I have I have a list of json structure such as
{ "S1" => "foo", "R2" => "bar", "S2" => "baz" }
and I want to get the data for the "S*" keys,
How would you do that in ruby? Is there any way to perform such a task?
Thanks,
Use select to pick out the key/value pairs you want:
{ "S1" => "foo", "R2" => "bar", "S2" => "baz" }.select{|k,v| k =~ /^S/}
The result is the desired hash - if you are using Ruby 1.9/2.0. However, in Ruby 1.8 this will return an array of arrays - you can wrap this with Hash[] to turn it back to a hash:
start = { "S1" => "foo", "R2" => "bar", "S2" => "baz" }
Hash[start.select{|k,v| k =~ /^S/}]
Assuming that you have already parsed json to hash it can be done in this way:
{ "S1" => "foo", "R2" => "bar", "S2" => "baz" }.select {|k,v| k.match /^S.*/}
If this is still json, you will have to use JSON.parse before.
require "json"
JSON.parse(your_json)

Reverse a hash in Ruby

How would I reverse the elements in the hash, keeping the same values and keys, but reversing their order in the hash.
Like so:
{ "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
And convert that to:
{ "1" => "spider", "lala" => "54", "10" => "cool", "4" => "happiness" }
Or, perhaps I could run a each loop backwards, starting from the last element in the hash, rather than the first?
You could convert the Hash to an Array, reverse that, and then convert it back to a Hash:
reversed_h = Hash[h.to_a.reverse]
Hash#to_a gives you an array of arrays, the inner arrays are simple [key,value] pairs, then you reverse that array using Array#reverse, and Hash[] converts the [key,value] pairs back into a Hash.
Ruby 2.1 adds an Array#to_h method so you can now say:
reversed_h = h.to_a.reverse.to_h
In Ruby 2.1+ you can combine reverse_each and to_h:
{foo: 1, bar: 2}.reverse_each.to_h
#=> {:bar=>2, :foo=>1}
In pure ruby, you can do it by hash.map(&:reverse).to_h or hash.reverse_each.to_h
In rails, you can do it by hash.invert
hash = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
reversed_hash = Hash[hash.to_a.reverse]
h = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
p Hash[h.reverse_each.map{|e| e}]
#=> {"1"=>"spider", "lala"=>"54", "10"=>"cool", "4"=>"happiness"}
But this leaves a bad taste (just like the other answers, which work fine just like this one). If you have to do this, it could be an indication that a Hash was not the best choice.
Alternatively, you can use reduce and merge to add the item to the front of a new hash:
hash = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
hash.reduce({}){ |memo, object| Hash[*object].merge(memo) }
but, that's crazy :D
reversed_h = Hash[h.to_a.collect(&:reverse)]
In Ruby 1.8.7, the order of elements in a hash is documented to be not under our control, so none of the above methods work. In Ruby 1.9.3, things work and are documented in the way that the other answers rely upon.
$ irb1.8
h = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
Hash[h.to_a().reverse()]
=> {"lala"=>"54", "1"=>"spider", "10"=>"cool", "4"=>"happiness"}
quit
$ irb1.9.1
h = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
Hash[h.to_a().reverse()]
=>{"1"=>"spider", "lala"=>"54", "10"=>"cool", "4"=>"happiness"}
The Ruby 1.8.7 way was ingrained so firmly for me that I misunderstood the question for quite some time. I thought it requested a way to Hash#invert: ie to transform the hash such that the range maps to the domain. That method discards duplicates. Luís Ramalho proffers a method that doesn't, but it's a bit clunky. This is a little shorter:
$ irb
def invertWithDuplicates(original)
inverse = Hash.new() { |hash, key| hash[key] = []; }
original.each_pair() { |key, value| inverse[value].push(key); }
return inverse
end
h = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "cool" }
invertWithDuplicates(h)
=> {"happiness"=>["4"], "cool"=>["1", "10"], "54"=>["lala"]}
Sorry to drift away from the OP's intended topic, though I submit that this does fit the post's title "Reverse a hash in Ruby".
if need:
hash = {:a => :x, :b => :y, :c => :y, :d => :z}
to:
{:x => [:a], :y => [:b, c], :z => [:d] }
can:
h={};hash.to_a.each{|e|h[e[1]]||=[];h[e[1]]<<e[0]};h

Resources