Construct a hash with other hashes - ruby

I want to join 2 or more hashes like this.
h1 = { :es => { :hello => "You" } }
h2 = { :es => { :bye => "Man" } }
How can I get this?
h1 + h2 = { :es => { :hello => "you", :bye => "Man" } }
Thanks.

irb(main):001:0> h1 = {:es => {:hello => "You"}}
=> {:es=>{:hello=>"You"}}
irb(main):002:0> h2 = {:es => {:bye => "Man"}}
=> {:es=>{:bye=>"Man"}}
irb(main):003:0> h1.each_key {|x| h1[x].merge! h2[x]}
=> {:es=>{:bye=>"Man", :hello=>"You"}}

What you want is the deep_merge method. Does exactly what you want.
ruby-1.9.2-p136 :001 > {:es => {:hello => "You" } }.deep_merge({:es => {:bye => "Man"}})
=> {:es=>{:hello=>"You", :bye=>"Man"}}
http://apidock.com/rails/ActiveSupport/CoreExtensions/Hash/DeepMerge/deep_merge

Similar to activesupport's deep_merge, but with a functional approach. Works recursively:
class Hash
def inner_merge(other_hash)
other_hash.inject(self) do |acc, (key, value)|
if (acc_value = acc[key]) && acc_value.is_a?(Hash) && value.is_a?(Hash)
acc.merge(key => acc_value.inner_merge(value))
else
acc.merge(key => value)
end
end
end
end
h1.inner_merge(h2) #=> {:es=>{:hello=>"You", :bye=>"Man"}}

If you don't use ActiveSupport, this Proc will perform a deep merge. 1.8.7 & 1.9.2 compatible.
dm = lambda {|l,r| l.merge(r) {|k,ov,nv| l[k] = ov.is_a?(Hash) ? dm[ov, nv || {}] : nv} }
dm[h1,h2]
# => {:es=>{:hello=>"You", :bye=>"Man"}}

Related

Access to merged cells using Ruby-Roo

According to example below: Value is stored only in A1, other cells return nil.
How is possible to get the A1'a value from the others merged cells, or simply check range of the A1 cell?
here is my take, if all merged fields are same as prev - then non-merged fields should become array
xlsx = Roo::Excelx.new(__dir__ + "/output.xlsx", { expand_merged_ranges: true })
parsed = xlsx.sheet(0).parse(headers: true).drop(1)
parsed_merged = []
.tap do |parsed_merged|
parsed.each do |x|
if parsed_merged.empty?
parsed_merged << {
"field_non_merged1" => x["field_non_merged1"],
"field_merged1" => [x["field_merged1"]],
"field_merged2" => [x["field_merged2"]],
"field_merged3" => [x["field_merged3"]],
"field_merged4" => [x["field_merged4"]],
"field_non_merged2" => x["field_non_merged2"],
"field_non_merged3" => x["field_non_merged3"],
}
else
field_merged1_is_same_as_prev = x["field_non_merged1"] == parsed_merged.last["field_non_merged1"]
field_merged2_is_same_as_prev = x["field_non_merged2"] == parsed_merged.last["field_non_merged2"]
field_merged3_is_same_as_prev = x["field_non_merged3"] == parsed_merged.last["field_non_merged3"]
merged_rows_are_all_same_as_prev = field_non_merged1_is_same_as_prev && field_merged2_is_same_as_prev && field_merged3_is_same_as_prev
if merged_rows_are_all_same_as_prev
parsed_merged.last["field_merged1"].push x["field_merged1"]
parsed_merged.last["field_merged2"].push x["field_merged2"]
parsed_merged.last["field_merged3"].push x["field_merged3"]
parsed_merged.last["field_merged4"].push x["field_merged4"]
else
parsed_merged << {
"field_non_merged1" => x["field_non_merged1"],
"field_merged1" => [x["field_merged1"]],
"field_merged2" => [x["field_merged2"]],
"field_merged3" => [x["field_merged3"]],
"field_merged4" => [x["field_merged4"]],
"field_non_merged2" => x["field_non_merged2"],
"field_non_merged3" => x["field_non_merged3"],
}
end
end
end
end
.map do |x|
{
"field_non_merged1" => x["field_non_merged1"],
"field_merged1" => x["field_merged1"].compact.uniq,
"field_merged2" => x["field_merged2"].compact.uniq,
"field_merged3" => x["field_merged3"].compact.uniq,
"field_merged4" => x["field_merged4"].compact.uniq,
"field_non_merged2" => x["field_non_merged2"],
"field_non_merged3" => x["field_non_merged3"],
}
end
This is not possible without first assigning the value to all the cells of the range, even in Excel VBA this is the case.
See this sample
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
sheet.add_row ["Val", nil]
sheet.add_row [nil, nil]
merged = sheet.merge_cells('A1:B2')
p sheet.rows[0].cells[0].value # "Val"
p sheet.rows[0].cells[1].value # nil
sheet[*merged].each{|cell|cell.value = sheet[*merged].first.value}
p sheet.rows[0].cells[0].value # "Val"
p sheet.rows[0].cells[1].value # "Val"
end
p.serialize('./simple.xlsx')
Please add a sample yourself next time so that we see which gem you used, which code, error etc.

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

Merge values in a Hash

nodes = {
:node1 => {
:inherits => nil,
:variables => { :foo => 1, :bar => 2 }
},
:node2 => {
:inherits => :node1,
:variables => { :foo => 9, :baz => 4 }
}
}
I've been trying to understand how to return a new nodes hash where each node's :variables hash is merged with :variables from the node specified in :inheritance. In other words, node1 would be left as is while node2 to end up with :variabes => { :foo => 9, :bar => 2, :baz => 4 }
I've been swimming through docs involving Enumerable#inject, Hash#merge with a block, and more and figure it's time to ask for help.
UPDATE:
Figured I'd provide an update. This code is certainly not the solution but it might be heading in the right direction...
nodes = {
:node1 => { :inherits => nil, :variables => { :foo => 1, :bar => 2 } },
:node2 => { :inherits => :node1, :variables => { :foo => 9, :baz => 4 } }
}
new = nodes.inject({}) do |result, (k, v)|
result.merge k => v.merge({ :variables => { :a => 6, :b => 7 } })
end
returns
{:node2=>{:inherits=>:node1, :variables=>{:a=>6, :b=>7}}, :node1=>{:inherits=>nil, :variables=>{:a=>6, :b=>7}}}
So that v.merge is not working as intended...
You want Hash#merge:
merged_variables = nodes[:node1][:variables].merge(nodes[:node2][:variables])
nodes[:node2][:variables].replace(
nodes[:node1][:variables]
.merge(nodes[:node2][:variables])
)
merged_nodes = {}
nodes.each do |name, node|
merged_nodes[name] = node.dup
merged_nodes[name][:variables] = if node[:inherits]
nodes[node[:inherits]][:variables].merge node[:variables]
else
node[:variables].dup
end
end
will give you
{
:node1=>{:inherits=>nil, :variables=>{:foo=>1, :bar=>2}},
:node2=>{:inherits=>:node1, :variables=>{:foo=>9, :bar=>2, :baz=>4}}
}
but it won't handle deeper nesting i.e. if :node3 then inherits :node2... if you need anything as complicated as that, this hash-based approach is going to get pretty clunky.
A little bit shorter by merging in place:
nodes[:node2][:variables].merge!(nodes[:node1][:variables])
nodes #=> {:node1=>{:inherits=>nil, :variables=>{:foo=>1, :bar=>2}},
# :node2=>{:inherits=>:node1, :variables=>{:foo=>1, :baz=>4, :bar=>2}}}
new = {}
nodes.each do |e, v|
v.each do |attribute, value|
if attribute == :inherits
new[e] = value.nil? ? v : nodes[value][:variables].merge( v[:variables] )
end
end
end
p new #=> {:node1=>{:inherits=>nil, :variables=>{:foo=>1, :bar=>2}}, :node2=>{:foo=>9, :bar=>2, :baz=>4}}

How to merge Ruby hashes

How can I merge these two hashes:
{:car => {:color => "red"}}
{:car => {:speed => "100mph"}}
To get:
{:car => {:color => "red", :speed => "100mph"}}
There is a Hash#merge method:
ruby-1.9.2 > a = {:car => {:color => "red"}}
=> {:car=>{:color=>"red"}}
ruby-1.9.2 > b = {:car => {:speed => "100mph"}}
=> {:car=>{:speed=>"100mph"}}
ruby-1.9.2 > a.merge(b) {|key, a_val, b_val| a_val.merge b_val }
=> {:car=>{:color=>"red", :speed=>"100mph"}}
You can create a recursive method if you need to merge nested hashes:
def merge_recursively(a, b)
a.merge(b) {|key, a_item, b_item| merge_recursively(a_item, b_item) }
end
ruby-1.9.2 > merge_recursively(a,b)
=> {:car=>{:color=>"red", :speed=>"100mph"}}
Hash#deep_merge
Rails 3.0+
a = {:car => {:color => "red"}}
b = {:car => {:speed => "100mph"}}
a.deep_merge(b)
=> {:car=>{:color=>"red", :speed=>"100mph"}}
Source: https://speakerdeck.com/u/jeg2/p/10-things-you-didnt-know-rails-could-do
Slide 24
Also,
http://apidock.com/rails/v3.2.13/Hash/deep_merge
You can use the merge method defined in the ruby library. https://ruby-doc.org/core-2.2.0/Hash.html#method-i-merge
Example
h1={"a"=>1,"b"=>2}
h2={"b"=>3,"c"=>3}
h1.merge!(h2)
It will give you output like this {"a"=>1,"b"=>3,"c"=>3}
Merge method does not allow duplicate key, so key b will be overwritten from 2 to 3.
To overcome the above problem, you can hack merge method like this.
h1.merge(h2){|k,v1,v2|[v1,v2]}
The above code snippet will be give you output
{"a"=>1,"b"=>[2,3],"c"=>3}
h1 = {:car => {:color => "red"}}
h2 = {:car => {:speed => "100mph"}}
h3 = h1[:car].merge(h2[:car])
h4 = {:car => h3}

How to change format of nested hashes

I'm looking for a solution how to write the format function which will take a string or nested hash as an argument and return the flatten version of it with the path as a key.
arg = "foo"
format(arg) # => { "hash[keys]" => "foo" }
arg = {:a => "foo", :b => { :c => "bar", :d => "baz" }}
format(arg) # => { "hash[keys][a]" => "foo", "hash[keys][b][c]" => "bar", "hash[keys][b][d]" => "baz" }
def hash_flatten h
h.inject({}) do |a,(k,v)|
if v.is_a?(Hash)
hash_flatten(v).each do |sk, sv|
a[[k]+sk] = sv
end
else
k = k ? [k] : []
a[k] = v
end
a
end
end
def format h
if h.is_a?(Hash)
a = hash_flatten(h).map do |k,v|
key = k.map{|e| "[#{e}]"}.join
"\"event[actor]#{key}\" => \"#{v}\""
end.join(', ')
else
format({nil => h})
end
end
arg = "sth"
puts format(arg)
# => "event[actor]" => "sth"
arg = {:a => "sth", :b => { :c => "sth else", :d => "trololo" }}
puts format(arg)
# => "event[actor][a]" => "sth", "event[actor][b][c]" => "sth else", "event[actor][b][d]" => "trololo"

Resources