Find the (deep) complement of two hashes - ruby

The complement is the mathematical term for what I'm looking for, but for context and possibly more targeted solution: I have hash A, which can have nested hashes (i.e. they're N-dimensional), and I apply to it a process (over which I have no control) which returns hash B, which is hash A with some elements removed. From there on, I am trying to find the elements in A which have been removed in B.
For example: (note that I use symbols for simplicity. Keys will always be symbols, but values won't.)
a = {:a => :b,
:c => {:d => :e, :f => :g},
:h => :i,
:j => {:k => :l, :m => :n},
:o => {:p => :q, :r => :s},
:t => :u}
b = {:h => :i,
:j => {:k => :l, :m => :n},
:o => {:r => :s},
:t => :u}
complement(a,b)
#=> {:a => :b,
# :c => {:d => :e, :f => :g},
# :o => {:p => :q}}
What is the best (ruby-esque) way of doing this?

Came up with this
a = {a: "thing", b: [1,2,3], c:2}
b = {a: "thing", b: [1,2,3]}
c= {}
a.each do |k, v|
c[k] = v unless b[k]
end
p c
EDIT: Now checking nested hashes. But yes, there should be some better ruby way to do this.
def check_deleted(a, b)
c = Hash.new
a.each do |k, v|
if ! b.has_key? k
c[k] = v
elsif b[k].is_a? Hash
c[k] = check_deleted(v, b[k])
end
end
c
end
a = {a: "thing", b: [1,2,3], c:2, d: {e: 1, r:2}}
b = {a: "thing", b: [1,2,3], d: {r:2}}
p check_deleted(a,b) #=> {:c=>2, :d=>{:e=>1}}

Related

Ruby - Merge an Array into a Hash

I have an array which looks like this:
array = [[:foo, :bar], [:foo, :baz], [:baz, {a: 1, b: 2}], [:baz, {c: 1, d:2}]]
and I need to turn it into a hash which looks like this:
{:foo =>[:bar, :baz], :baz => {a: 1, b: 2, c: 1, d: 2}}
This is the code I have so far:
def flatten(array)
h = {}
array.each_with_object({}) do |(k, v), memo|
if v.is_a?(Hash)
memo[k] = h.merge!(v)
else
# What goes here?
end
end
end
When used like so:
flatten(array)
outputs:
{baz => {:a => 1, :b => 2, :c => 1, :d => 2}}
May someone please point me in the right direction? Help appreciated.
def convert(arr)
arr.each_with_object({}) do |a,h|
h[a.first] =
case a.last
when Hash
(h[a.first] || {}).update(a.last)
else
(h[a.first] || []) << a.last
end
end
end
convert array
#=> {:foo=>[:bar, :baz], :baz=>{:a=>1, :b=>2, :c=>1, :d=>2}}
Hash[ array.group_by(&:first).map{ |k,a| [k,a.map(&:last)] } ]
Here is my attempt at solving this problem. I have to make assumption that in the input array, entries like the ones similar to :baz will always be paired with Hash objects. The solution will not work if you have one :baz with a symbol and another with hash.
array = [[:foo, :bar], [:foo, :baz], [:baz, {a: 1, b: 2}], [:baz, {c: 1, d:2}]]
h = Hash.new
array.each do |n1, n2|
if n2.class == Hash
h[n1] = (h[n1] || {}).merge(n2)
else
h[n1] = (h[n1] || []) << n2
end
end
p h
Output
{:foo=>[:bar, :baz], :baz=>{:a=>1, :b=>2, :c=>1, :d=>2}}
[Finished in 0.1s]

Ruby - Hash to methods recursively

I have a hash-of-hashes and I need to turn it into methods where they key is the method name, and the value is what the method returns, but with the extra requirement that if a value is a also a hash, the whole value is returned.
For instance:
hash = {:a => 1, :b => 2, :c => { :a => 1, :b => 2, :c => 3} }
hash.c.a #=> 1
hash.c #=> { :a => 1, :b => 2, :c => 3}
How would I be able to do this? Help appreciated.
You could use OpenStruct to make a data structure like that. For example:
require 'ostruct'
s = OpenStruct.new(a: 1, b: 2, c: OpenStruct.new(a: 1, b: 2, c: 3))
s.c.a # => 1
s.c # => #<OpenStruct a=1, b=2, c=3>
Another option is to use Hashie. Hashie is a set of hash-like extensions. Among those are hashes that you can access directly like fields - see specifically the Mash option:
mash = Hashie::Mash.new(:a => 1, :b => 2, :c => Hashie::Mash.new(:a => 1, :b => 2, :c => 3))
As your receiver is a hash, you must define the methods on the class Hash:
def makem(h)
h.each do |k,v|
case v
when Hash
Hash.instance_eval { define_method(k.to_s) { v } }
v.each { |kk,vv| Hash.instance_eval { define_method(kk.to_s) { vv } } }
end
end
end
Let's try it:
h = {:a => 1, :b => 2, :c => { :a => 1, :b => 2, :d => 3 } }
makem(h)
Hash.instance_methods(false).select { |m| m.size == 1 }
#=> [:c, :a, :b, :d]
h.c
#=> {:a=>1, :b=>2, :d=>3}
h.c.a
#=> 1
h.c.b
#=> 2
h.c.d
#=> 3
In your example, you have:
:c => { :a => 1, :b => 2, :c => 3}
If you want a method c that returns the above hash, you obviously cannot define another method of the same name that would return 3.
You may wish to reconsider the advisability of defining these methods. Perhaps it would be more useful to construct methods such as def m(hash, key) within your class.

Ruby creating Hash custom invert function in Ruby

Ruby class Hash has method "invert" which make "reversal" between keys and values and delete same keys (in our case its: "1=>:a").
h = {a: 1, b: 2, c: 1}
=> {:a=>1, :b=>2, :c=>1}
h.invert
=> {1=>:c, 2=>:b}
How implement custom Hash method "c_invert", which will return very first (not last) pair of duplicated key => value? Exapmle:
> h = {a: 1, b: 2, c: 1}
=> {:a=>1, :b=>2, :c=>1}
> h.c_invert
=> {1=>:a, 2=>:b}
class Hash
def c_invert
Hash[to_a.reverse].invert
end
end
or
class Hash
def c_invert
Hash[to_a.reverse.map(&:reverse)]
end
end
h = {:d =>1,:a=>1, :b=> 2, :c=>1}
Hash[h.map(&:reverse).reverse]
# => {1=>:d, 2=>:b}
h = {a: 1, b: 2, c: 1}
Hash[h.map(&:reverse).reverse]
# => {1=>:a, 2=>:b}

How do I intersect one hash's keys with another and filter out the matching values?

I got this from the Rails doc:
{1 => 2}.diff(1 => 2) # => {}
{1 => 2}.diff(1 => 3) # => {1 => 2}
{}.diff(1 => 2) # => {1 => 2}
{1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
This is almost perfect but I don't want values that are in the hash passed as parameter and not in the calling hash.
What I want:
{}.diff(1 => 2) # => {}
{a: 1}.diff({a: 1, b: 2}) # => {} instead of {:b => 2}
Also, it must be as efficient as possible. For instance, I don't want to go over the second hash and check that each key that's inside doesn't appear in the first.
Any ideas?
Looking at the source is helpful here.
def diff(h2)
dup.delete_if { |k, v| h2[k] == v }.merge!(h2.dup.delete_if { |k, v| has_key?(k) })
end
Which iterates over every entry in the hash. I'm assuming you don't want to add an unnecessary iteration. So it's easier than the above
def my_diff(h2)
dup.delete_if { |k, v| h2[k] == v }
end
This is pretty easy:
a.select {|k, v| b.key?(k) && b[k] != v }
This is O(n), since both key? and Hash#[] are both O(1).

How do I map an array of hashes?

I have an array of hashes:
arr = [ {:a => 1, :b => 2}, {:a => 3, :b => 4} ]
What I want to achieve is:
arr.map{|x| x[:a]}.reduce(:+)
but I think it's a bit ugly, or at least not that elegant as:
arr.map(&:a).reduce(:+)
The later one is wrong because there is no method called a in the hashes.
Are there any better ways to write map{|x| x[:a]}?
You could make actual Objects, possibly with a Struct:
MyClass = Struct.new :a, :b
arr = [MyClass.new(1, 2), MyClass.new(3, 4)]
arr.map(&:a).reduce(:+) #=> 4
Or for more flexibility, an OpenStruct:
require 'ostruct'
arr = [OpenStruct.new(a: 1, b: 2), OpenStruct.new(a: 3, b: 4)]
arr.map(&:a).reduce(:+) #=> 4
Of course either of these can be constructed from existing hashes:
arr = [{ :a => 1, :b => 2 }, { :a => 3, :b => 4 }]
ss = arr.map { |h| h.values_at :a, :b }.map { |attrs| MyClass.new(*attrs) }
ss.map(&:a).reduce(:+) #=> 4
oss = arr.map { |attrs| OpenStruct.new attrs }
oss.map(&:a).reduce(:+) #=> 4
Or, for a more creative, functional approach:
def hash_accessor attr; ->(hash) { hash[attr] }; end
arr = [{ :a => 1, :b => 2 }, { :a => 3, :b => 4 }]
arr.map(&hash_accessor(:a)).reduce(:+) #=> 4
It is unclear what you mean as "better" and why you think the correct version is ugly.
Do you like this "better"?
arr.inject(0) { |sum, h| sum + h[:a] }
There's a way, extending the Symbol.
lib/core_extensions/symbol.rb (credit goes here)
# frozen_string_literal: true
class Symbol
def with(*args, &)
->(caller, *rest) { caller.send(self, *rest, *args, &) }
end
end
Then, given:
arr = [ {:a => 1, :b => 2}, {:a => 3, :b => 4} ]
you can do this:
arr.map(&:[].with(:a)).reduce(:+)
Explanation: to access hash value under any key, you call Hash#[] method. When passed as a :[] (extended) symbol to the Array#map, you can then call .with(*args) on this symbol, effectively passing the parameter (hash key) down to the :[] method. Enjoy.

Resources