Concatenating tree key names [duplicate] - ruby

This question is the inverse of this question.
Given a nested hash like
{
:a => {
:b => {:c => 1, :d => 2},
:e => 3,
},
:f => 4,
}
what is the best way to convert it into a flat hash like
{
[:a, :b, :c] => 1,
[:a, :b, :d] => 2,
[:a, :e] => 3,
[:f] => 4,
}

Another way:
def flat_hash(h,f=[],g={})
return g.update({ f=>h }) unless h.is_a? Hash
h.each { |k,r| flat_hash(r,f+[k],g) }
g
end
h = { :a => { :b => { :c => 1,
:d => 2 },
:e => 3 },
:f => 4 }
flat_hash(h) #=> {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}

Very similar to Adiel Mittmann's solution
def flat_hash(h, k = [])
new_hash = {}
h.each_pair do |key, val|
if val.is_a?(Hash)
new_hash.merge!(flat_hash(val, k + [key]))
else
new_hash[k + [key]] = val
end
end
new_hash
end
Edit: Refactored for elegance. Should be almost as fast.
def flat_hash(hash, k = [])
return {k => hash} unless hash.is_a?(Hash)
hash.inject({}){ |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end

My attempt:
def flatten_hash(h)
return { [] => h } unless h.is_a?(Hash)
Hash[h.map { |a,v1| flatten_hash(v1).map { |b,v2| [[a] + b, v2] } }.flatten(1)]
end
Sorry for the bad variables names, had to fit it in one line.

This is not an attempt to give you the best way to do it, but it is a way :P
def flatten(hash)
return {[] => hash} if !hash.is_a?(Hash)
map = {}
hash.each_pair do |key1, value1|
flatten(value1).each_pair do |key2, value2|
map[[key1] + key2] = value2
end
end
return map
end
It works for your example, producing this result:
{[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
It may not produce the result you expect if there are empty hashes.

A functional approach (see the history for an alternative implementations):
def recursive_flatten(hash)
hash.flat_map do |key, value|
if value.is_a?(Hash)
recursive_flatten(value).map { |ks, v| [[key] + ks, v] }
else
[[[key], value]]
end
end.to_h
end

Inspired by #cary-swoveland way, but in Hash class :
class Hash
def deep_flatten(previous_key=[])
flat_hash = {}
self.each do |key, value|
next_key = previous_key+[key]
flat_hash.update(value.is_a?(Hash) ? value.deep_flatten(next_key) : {next_key=>value})
end
return flat_hash
end
end
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 }
h.deep_flatten #=> {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}

A declarative solution using DeepEnumerable:
require 'deep_enumerable'
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 }
h.deep_each.map do |k, v|
[DeepEnumerable.deep_key_to_array(k), v]
end.to_h
or, for those who prefer point-free style
h.deep_each.to_h.shallow_map_keys(&DeepEnumerable.method(:deep_key_to_array))

Array support / readable names / no update for speed / stringified results keys
def flat_hash(input, base = nil, all = {})
if input.is_a?(Array)
input = input.each_with_index.to_a.each(&:reverse!)
end
if input.is_a?(Hash) || input.is_a?(Array)
input.each do |k, v|
flat_hash(v, base ? "#{base}.#{k}" : k, all)
end
else
all[base] = input
end
all
end

Related

transform keys and values with an array as one of them

I have the following Ruby hash
a = {
7 => [1469, 2283],
5 => [1469, 5464],
3 => [7561],
6 => [7952, 8114],
4 => []
}
and would like to get the keys that a number shows up in as a value
b = {
1469 => [7,5],
2283 => [7],
5464 => [5]
...
}
How would I do this? I'm sure there's some super slick way of getting it done.
Given:
a = {
7 => [1469, 2283],
5 => [1469, 5464],
3 => [7561],
6 => [7952, 8114],
4 => [1469, 2283],
2 => []
}
Use a default value of a new array in b:
b=Hash.new {|hsh, key| hsh[key] = [] }
a.each{|k,v| v.each{|n| b[n] << k} }
Or include the object creation with each_with_object:
b=a.each_with_object(Hash.new {|h,k| h[k] = []}) { |(k,v), h|
v.each{ |e| h[e] << k } }
Result b is:
{1469=>[7, 5, 4], 2283=>[7, 4], 5464=>[5], 7561=>[3], 7952=>[6], 8114=>[6]}
Using #each_with_object to iterate over the original hash and the values stored in each array and build up a new hash based on it.
a.each_with_object({}) do |(k, v), h|
v.each do |val|
h[val] ||= []
h[val] << k
end
end
Result:
{
1469 => [7, 5],
2283 => [7],
5464 => [5],
7561 => [3],
7952 => [6],
8114 => [6]
}
#dawg's answer is the way I would go
a.each_with_object(Hash.new {|h,k| h[k] = []}) do |(k,v),obj|
v.each{|e| obj[e] << k}
end
But here are a few other options: (a lot of allocations though)
Array#product and Hash#merge
a.each_with_object({}) do |(k,v),obj|
obj.merge!(v.product([[k]]).to_h) {|_,o,v| [*o,*v]}
end
#map then #reduce
a.map {|k,v| v.product([[k]]).to_h }
.reduce {|memo, h| memo.merge(h) {|_,o,n| o.concat(n)}}
This works too (don't ask questions)
a.map {|k,v| v.each_with_object(k)}
.reduce(&:+)
.group_by(&:shift)
.transform_values(&:flatten)

Instance initialization taking a hash

Most of the online material has initialization like this:
class MyClass
attr_accessors :a, :b, :c
def initialize(a,b,c)
#a = a
#b = b
#c = c
end
end
with or without defaults. Creating a new instance is:
n = MyClass.new(1,2,3)
n.a # => 1
n.b # => 2
n.c # => 3
I would like to know how to initialize the instance with hash syntax, like:
n = MyClass.new(:a => 1, :b => 2, :c => 3)
which should be equivalent to:
n = MyClass.new(:b => 2, :a => 1, :c => 3)
Is this difficult to implement?
Use ruby keyword arguments, available in Ruby 2.0 and above
Format in initializer arguments is variable:
class MyClass
attr_accessors :a, :b, :c
def initialize(a:, b:, c: )
#a = a
#b = b
#c = c
end
end
Are you looking for the following?
class MyClass
attr_accessor :hash
def initialize(hash)
#hash = hash
end
end
e1 = MyClass.new(:a => 1, :b => 2, :c => 3)
#=> #<MyClass:0x007f88240d66b0 #hash={:a=>1, :b=>2, :c=>3}>
e1.hash
#=> {:a=>1, :b=>2, :c=>3}
e2 = MyClass.new( { :a => 1, :b => 2, :c => 3 } )
#=> #<MyClass:0x007f882281d678 #hash={:c=>3, :b=>2, :a=>1}>
e2.hash
#=> {:a=>1, :b=>2, :c=>3}
e3 = MyClass.new(:b => 2, :a => 1, :c => 3)
#=> #<MyClass:0x007f88240bd408 #hash={:b=>2, :a=>1, :c=>3}>
e3.hash
#=> {:b=>2, :a=>1, :c=>3}
Are e1, e2 and e3 "equivalent"?
e1 == e2
#=> false
e1 == e3
#=> false
e1.hash == e2.hash
#=> true
e1.hash == e3.hash
#=> true
Another way of looking at this is as follows:
class MyClass
attr_accessor :args
def initialize(*args)
#args = args
end
end
e1 = MyClass.new(:a => 1, :b => 2, :c => 3)
#=> #<MyClass:0x007f88240d66b0 #hash={:a=>1, :b=>2, :c=>3}>
e1.args
#=> [{:a=>1, :b=>2, :c=>3}]
which shows that only a single argument (a hash) is being passed to new.
It can be as simple as:
require 'ostruct'
class MyClass < OpenStruct
end
n = MyClass.new(:a => 1, :b => 2, :c => 3)
n.a
# => 1

How to replace all hash keys having a '.'?

I am using Ruby on Rails 4 and I would like to replace all hash keys so to change the hash from
h_before = {:"aaa.bbb" => 1, :c => 2, ...}
to
h_after = {:bbb => 1, :c => 2, ...}
That is, I would like to someway "demodulize" all hash keys having the .. How can I make that?
each_with_object is a cleaner and shorter approach than inject from the answer:
h_before.each_with_object({}){|(k, v),h| h[k.to_s.split(".").last.to_sym] = v}
=> {:bbb=>1, :c=>2}
h_before = {:"aaa.bbb" => 1, :c => 2}
h_after =
h_before.inject({}){|h, (k, v)| h[k.to_s.split(".").last.to_sym] = v; h}
# => {:bbb = > 1, :c => 2}
Since there are a bunch of answers claiming to do the same thing, I thought it was time to post some benchmarks:
require 'fruity'
h_before = {:"aaa.bbb" => 1, :c => 2}
def cdub_test(hash)
Hash[hash.map {|k, v| [k.to_s.gsub(/^.*\./,"").to_sym, v]}]
end
def matt_test(old_hash)
Hash[old_hash.map { |k,v| [ k.to_s.sub(/.*\./,'').to_sym, v ] }]
end
class Hash
require 'active_support/core_ext/hash/indifferent_access'
def grep_keys(pattern)
return inject(HashWithIndifferentAccess.new){|h, (k, v)|
h[$1 || k] = v if pattern =~ k.to_s ; h }
end
end
def phlip_test(hash)
hash.grep_keys(/\.(\w+)$/)
end
def bjhaid_test(hash)
hash.each_with_object({}){|(k, v),h| h[k.to_s.split(".").last.to_sym] = v}
end
def sawa_test(hash)
hash.inject({}){|h, (k, v)| h[k.to_s.split(".").last.to_sym] = v; h}
end
compare do
cdub { cdub_test(h_before) }
matt { matt_test(h_before) }
phlip { phlip_test(h_before) }
bjhaid { bjhaid_test(h_before) }
sawa { sawa_test(h_before) }
end
Which outputs:
Running each test 1024 times. Test will take about 1 second.
bjhaid is similar to sawa
sawa is faster than matt by 60.00000000000001% ± 10.0%
matt is faster than phlip by 30.000000000000004% ± 10.0% (results differ: {:bbb=>1, :c=>2} vs {"bbb"=>1})
phlip is similar to cdub (results differ: {"bbb"=>1} vs {:bbb=>1, :c=>2})
Notice that phlip's code doesn't return the desired results.
old_hash = {:"aaa.bbb" => 1, :c => 2 }
new_hash = Hash[old_hash.map { |k,v| [ k.to_s.sub(/.*\./,'').to_sym, v ] }]
1.9.3p448 :001 > hash = {:"aaa.bbb" => 1, :c => 2 }
=> {:"aaa.bbb"=>1, :c=>2}
1.9.3p448 :002 > Hash[hash.map {|k, v| [k.to_s.gsub(/^.*\./,"").to_sym, v]}]
=> {:bbb=>1, :c=>2}
My grep_keys has never failed me here:
class Hash
def grep_keys(pattern)
return inject(HashWithIndifferentAccess.new){|h, (k, v)|
h[$1 || k] = v if pattern =~ k.to_s ; h }
end
end
It returns a shallow-copy of the Hash, but only with the matched keys. If the input regular expression contains a () match, the method replaces the key with the matched value. (Note this might merge two or more keys, and discard all but a random value for them!) I use it all the time to cut up a Rails param into sub-params containing only the keys that some module needs.
{:"aaa.bbb" => 1, :c => 2 }.grep_keys(/\.(\w+)$/) returns {"bbb"=>1}.
This method upgrades your actual problem to "how to define a regexp that matches what you mean by 'having a ".".'"

Converting a nested hash into a flat hash

This question is the inverse of this question.
Given a nested hash like
{
:a => {
:b => {:c => 1, :d => 2},
:e => 3,
},
:f => 4,
}
what is the best way to convert it into a flat hash like
{
[:a, :b, :c] => 1,
[:a, :b, :d] => 2,
[:a, :e] => 3,
[:f] => 4,
}
Another way:
def flat_hash(h,f=[],g={})
return g.update({ f=>h }) unless h.is_a? Hash
h.each { |k,r| flat_hash(r,f+[k],g) }
g
end
h = { :a => { :b => { :c => 1,
:d => 2 },
:e => 3 },
:f => 4 }
flat_hash(h) #=> {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
Very similar to Adiel Mittmann's solution
def flat_hash(h, k = [])
new_hash = {}
h.each_pair do |key, val|
if val.is_a?(Hash)
new_hash.merge!(flat_hash(val, k + [key]))
else
new_hash[k + [key]] = val
end
end
new_hash
end
Edit: Refactored for elegance. Should be almost as fast.
def flat_hash(hash, k = [])
return {k => hash} unless hash.is_a?(Hash)
hash.inject({}){ |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end
My attempt:
def flatten_hash(h)
return { [] => h } unless h.is_a?(Hash)
Hash[h.map { |a,v1| flatten_hash(v1).map { |b,v2| [[a] + b, v2] } }.flatten(1)]
end
Sorry for the bad variables names, had to fit it in one line.
This is not an attempt to give you the best way to do it, but it is a way :P
def flatten(hash)
return {[] => hash} if !hash.is_a?(Hash)
map = {}
hash.each_pair do |key1, value1|
flatten(value1).each_pair do |key2, value2|
map[[key1] + key2] = value2
end
end
return map
end
It works for your example, producing this result:
{[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
It may not produce the result you expect if there are empty hashes.
A functional approach (see the history for an alternative implementations):
def recursive_flatten(hash)
hash.flat_map do |key, value|
if value.is_a?(Hash)
recursive_flatten(value).map { |ks, v| [[key] + ks, v] }
else
[[[key], value]]
end
end.to_h
end
Inspired by #cary-swoveland way, but in Hash class :
class Hash
def deep_flatten(previous_key=[])
flat_hash = {}
self.each do |key, value|
next_key = previous_key+[key]
flat_hash.update(value.is_a?(Hash) ? value.deep_flatten(next_key) : {next_key=>value})
end
return flat_hash
end
end
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 }
h.deep_flatten #=> {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
A declarative solution using DeepEnumerable:
require 'deep_enumerable'
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 }
h.deep_each.map do |k, v|
[DeepEnumerable.deep_key_to_array(k), v]
end.to_h
or, for those who prefer point-free style
h.deep_each.to_h.shallow_map_keys(&DeepEnumerable.method(:deep_key_to_array))
Array support / readable names / no update for speed / stringified results keys
def flat_hash(input, base = nil, all = {})
if input.is_a?(Array)
input = input.each_with_index.to_a.each(&:reverse!)
end
if input.is_a?(Hash) || input.is_a?(Array)
input.each do |k, v|
flat_hash(v, base ? "#{base}.#{k}" : k, all)
end
else
all[base] = input
end
all
end

Converting a hash into a nested hash

This question is the inverse of this question.
Given a hash that has an array for each key like
{
[:a, :b, :c] => 1,
[:a, :b, :d] => 2,
[:a, :e] => 3,
[:f] => 4,
}
what is the best way to convert it into a nested hash like
{
:a => {
:b => {:c => 1, :d => 2},
:e => 3,
},
:f => 4,
}
Here's an iterative solution, a recursive one is left as an exercise to the reader:
def convert(h={})
ret = {}
h.each do |k,v|
node = ret
k[0..-2].each {|x| node[x]||={}; node=node[x]}
node[k[-1]] = v
end
ret
end
convert(your_hash) # => {:f=>4, :a=>{:b=>{:c=>1, :d=>2}, :e=>3}}
Functional recursive algorithm:
require 'facets'
class Hash
def nestify
map_by { |ks, v| [ks.first, [ks.drop(1), v]] }.mash do |key, pairs|
[key, pairs.first[0].empty? ? pairs.first[1] : Hash[pairs].nestify]
end
end
end
p {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}.nestify
# {:a=>{:b=>{:c=>1, :d=>2}, :e=>3}, :f=>4}
There is already a good answer, but I worked on this recursive solution, so here it is:
def to_nest(hash)
{}.tap do |nest|
hash.each_pair do |key, value|
nodes = key.dup
node = nodes.shift
if nodes.empty?
nest[node] = value
else
nest[node] ||= {}
nest[node].merge!({nodes => value})
end
end
nest.each_pair do |key, value|
nest[key] = to_nest(value) if value.kind_of?(Hash)
end
end
end
Another way:
def convert(h)
h.each_with_object({}) { |(a,n),f| f.update({ a.first=>(a.size==1 ? n :
convert({ a[1..-1]=>n })) }) { |_,ov,nv| ov.merge(nv) } }
end
Try it:
h = {
[:a, :b, :c] => 1,
[:a, :b, :d] => 2,
[:a, :e] => 3,
[:f] => 4,
}
convert(h) #=> {:a=>{:b=>{:d=>2}, :e=>3},
# :f=>4}
For a mixed hash/array nested structure you can use this. (Modified for arrays as well)
def unflatten(h={})
ret = {}
h.each do |k,v|
node = ret
keys = k.split('.').collect { |x| x.to_i.to_s == x ? x.to_i : x }
keys.each_cons(2) do |x, next_d|
if(next_d.is_a? Fixnum)
node[x] ||= []
node=node[x]
else
node[x] ||={}
node=node[x]
end
end
node[keys[-1]] = v
end
ret
end
provided you used the below for flattening. ( dot separate string for key instead of array [split on . if you need] )
def flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a? Hash
flatten_hash(v).map do |h_k, h_v|
h["#{k}.#{h_k}"] = h_v
end
elsif v.is_a? Array
flatten_array(v).map do |h_k,h_v|
h["#{k}.#{h_k}"] = h_v
end
else
h[k] = v
end
end
end
def flatten_array(array)
array.each_with_object({}).with_index do |(v,h),i|
pp v,h,i
if v.is_a? Hash
flatten_hash(v).map do |h_k, h_v|
h["#{i}.#{h_k}"] = h_v
end
elsif v.is_a? Array
flatten_array(v).map do |h_k,h_v|
h["#{i}.#{h_k}"] = h_v
end
end
end
end
Using DeepEnumerable:
require DeepEnumerable
h = {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
h.inject({}){|hash, kv| hash.deep_set(*kv)}

Resources