Ruby - Hash to methods recursively - ruby

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.

Related

Is it possible to sort multidimensional hashes by child hash values?

Is is possible to sort parent hashes by values of children keys?
For example:
{
:a =>
{:order => 3},
:b =>
{:order => 1},
:c =>
{:order => 2}
}
resorted as
{
:b =>
{:order => 1},
:c =>
{:order => 2},
:a =>
{:order => 3}
}
You can convert it to an array of pairs, use sort_by method to target the value you want to sort by, and then convert it back to a hash:
h = {
:a =>
{:order => 3},
:b =>
{:order => 1},
:c =>
{:order => 2}
}
h.sort_by {|k,v| v[:order]}.to_h
=> {:b=>{:order=>1}, :c=>{:order=>2}, :a=>{:order=>3}}
Keep in mind that the only order that a Ruby hash can have is based on insertion order. You need to create a new hash (no sort!) and create the new hash element by element in the order you wish it to have.
Given:
> hash
=> {:a=>{:order=>3}, :b=>{:order=>1}, :c=>{:order=>2}}
You can use .sort_by to do:
> hash.sort_by {|k, h| h[:order]}.to_h
=> {:b=>{:order=>1}, :c=>{:order=>2}, :a=>{:order=>3}}
You can also use the more classic .sort with the spaceship <=> by unpacking the arguments associated with the usual a,b:
> hash.sort {|(a,ha),(b,hb)| ha[:order] <=> hb[:order] }.to_h
=> {:b=>{:order=>1}, :c=>{:order=>2}, :a=>{:order=>3}}
In either case, the .to_h method creates a new hash based on the sorted key, value pairs from the source hash.
Best

Sort hash by array

I have a hash and an array with same length like the following:
h = {:a => 1, :b => 2, :c => 3, :d => 4}
a = [2, 0, 1, 0]
I want to order the hash in increasing order of the values in the array. So the output would be something like:
h = {:b => 2, :d => 4, :c=> 3, :a => 1}
Ideally I want to introduce some randomness for ties. For the previous example, I want either the previous output or:
h = {:d => 4, :b => 2, :c=> 3, :a => 1}
This is what I tried.
b = a.zip(h).sort.map(&:last)
p Hash[b]
# => {:b=>2, :d=>4, :c=>3, :a=>1}
But I am not sure how to introduce the randomness.
h.to_a.sort_by.each_with_index{|el,i| [a[i], rand]}.to_h
You could modify what you have slightly:
def doit(h,a)
Hash[a.zip(h).sort_by { |e,_| [e,rand] }.map(&:last)]
end
doit(h,a) #=> { b=>2, d=>4, c=>3, a=>1 }
doit(h,a) #=> { d=>4, b=>2, c=>3, a=>1 }
doit(h,a) #=> { b=>2, d=>4, c=>3, a=>1 }
doit(h,a) #=> { b=>2, d=>4, c=>3, a=>1 }

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 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.

What is the behavior of ruby Hash#merge when used with a block

It does not seem to be documented very much:
hsh.merge(other_hash){|key, oldval, newval| block} → a_hash
http://ruby-doc.org/core/classes/Hash.html#M002880
As it might be expected, the resulting hash will contain the value returned by a block for every key which exists in both hashes being merged:
>> h1 = {:a => 3, :b => 5, :c => 6}
=> {:a=>3, :b=>5, :c=>6}
>> h2 = {:a => 4, :b => 7, :d => 8}
=> {:a=>4, :b=>7, :d=>8}
>> h1.merge h2
=> {:a=>4, :b=>7, :c=>6, :d=>8}
>> h1.merge(h2){|k,v1,v2| v1}
=> {:a=>3, :b=>5, :c=>6, :d=>8}
>> h1.merge(h2){|k,v1,v2| v1+v2}
=> {:a=>7, :b=>12, :c=>6, :d=>8}

Resources