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.
Related
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]
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 }
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.
hash = {:one => 1, :two => 2, :three => 3}
new_hash = hash.each { |k, v| v.to_int * 5 }
new_hash
I want this output:
=> {"one"=>5, "two"=>10, "three"=>15}
But I get this:
=> {"one"=>1, "two"=>2, "three"=>3}
Why?
each returns the original hash. You have to build a new one:
new_hash = hash.map { |k, v| [k, v * 5] }.to_h
#=> {:one=>5, :two=>10, :three=>15}
Or for Ruby < 2.1
new_hash = Hash[hash.map { |k, v| [k, v * 5] }]
#=> {:one=>5, :two=>10, :three=>15}
As you can see in the documentation, Hash#each returns the original hash, which you then assign to new_hash.
Do this instead:
hash = {:one => 1, :two => 2, :three => 3}
new_hash = Hash[hash.map { |k, v| [k, v * 5] }]
new_hash # => {:one=>5, :two=>10, :three=>15}
You can do as follows:
hash = {:one => 1, :two => 2, :three => 3}
new_hash = hash.reduce({}) { |h, (k, v) | h[ k ] = v.to_i * 5 ; h }
# => {:one=>5, :two=>10, :three=>15}
When a block is given, each method of Hash returns the original hash with no modification. See ruby doc.
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}