Ruby: Create hash with default keys + values of an array - ruby

I believe this has been asked/answered before in a slightly different context, and I've seen answers to some examples somewhat similar to this - but nothing seems to exactly fit.
I have an array of email addresses:
#emails = ["test#test.com", "test2#test2.com"]
I want to create a hash out of this array, but it must look like this:
input_data = {:id => "#{id}", :session => "#{session}",
:newPropValues => [{:key => "OWNER_EMAILS", :value => "test#test.com"} ,
{:key => "OWNER_EMAILS", :value => "test2#test2.com"}]
I think the Array of Hash inside of the hash is throwing me off. But I've played around with inject, update, merge, collect, map and have had no luck generating this type of dynamic hash that needs to be created based on how many entries in the #emails Array.
Does anyone have any suggestions on how to pull this off?

So basically your question is like this:
having this array:
emails = ["test#test.com", "test2#test2.com", ....]
You want an array of hashes like this:
output = [{:key => "OWNER_EMAILS", :value => "test#test.com"},{:key => "OWNER_EMAILS", :value => "test2#test2.com"}, ...]
One solution would be:
emails.inject([]){|result,email| result << {:key => "OWNER_EMAILS", :value => email} }
Update: of course we can do it this way:
emails.map {|email| {:key => "OWNER_EMAILS", :value => email} }

Related

Isolating and displaying a specific element within a hash

I am currently having trouble writing a test that addresses the eligibility_settings of a record I have. I am having trouble pulling out one of the specific elements from this hash.
Specifically I want to test that by making a change elsewhere in a different function that changes the min age of a specific player, and so what I am really trying to test is the eligibility_settings.min_age. But i'm having trouble within my test isolating that out.
My hash looks like this
{
:name => "player1",
:label => "redTeam_1_1",
:group => "adult",
:teamId => 7,
:eligibility_settings => {
"min_age" => 18,
"player_gender" => "female",
"union_players_only" => true
}
}
However when I try looping through this hash, I am having trouble isolating that one element.
i've tried something like
team.get_players.first.map do |settings, value|
value.tap do |x, y|
y[3]
end
end
However It seems like what i've been trying, and my approach has not been working quite right.
Would anyone have any idea what I could do with this?
Although #SergioTulentsev gave the proper response, in the future if you are going to be looping through hashes, below is one way to iterate through the keys and grab the value you want.
hash = {
:name => "player1",
:label => "redTeam_1_1",
:group => "adult",
:teamId => 7,
:eligibility_settings => {
"min_age" => 18,
"player_gender" => "female",
"union_players_only" => true
}
}
hash.map do |settings, value|
p hash[:eligibility_settings]['min_age'] if settings == :eligibility_settings
end # output 18

How do I reorganise a hash similar to a table transpose in Ruby?

Lets say I have the following hash that represents a multi-dimensional question and answer matrix:
{'Are you male?' => {'Yes' => {'Do you have children?' => {'No' => '75%', 'Yes' => '25%'}}}}
How would I programmatically reorganise the hash so that it looks like this:
{'Do you have children?' => {'No' => {'Are you male?' => {'Yes' => '75%'}}, 'Yes' => {'Are you male?' => {'Yes' => '25%'}}}}
Use the original excel sheet, if possible. If not, I'd do it via intermediate form. Flatten your hash to a CSV file/string:
Are you male?, Do you have children?, percentage
Yes, Yes, 25%
Yes, No, 75%
From this you can easily reconstruct whatever alternative nesting you need.
My solution:
Store the questions in combination as a hash key representing (Row)|(Column), and then put the actual options and values in an array. Then switch questions in the hash key and transpose the array. So:
{'Are you male?|Do you have children?' => [[nil, 'Yes', 'No'],['Yes', '25%', '75%']]}
becomes:
{'Do you have children?|Are you male?' => [[nil, "Yes"], ["Yes", "25%"], ["No", "75%"]]}

New hash from array of hashes

The objective of the code below is to produce a hash with the keys being the :id field of
the hashes in original_array, and the values being all elements in original_array which have that :id.
original_array = [
{:id => '123', :name => 'test'},
{:id => '123', :name => 'another test'},
{:id => '456', :name => 'yet another test'}
]
new_hash = {}
original_array.each do |a|
new_hash[a[:id]] = original_array.select {|x| x[:id] == a[:id]}
end
My code does that, but there must be some better way to do it, ideally where the hash can be created in one step. If anyone can suggest and explain one (in the hope that I might improve my understanding of this sort of thing), then it would be appreciated.
This should do it
new_hash = original_array.group_by{|h| h[:id]}
Documentation: Enumerable#group_by.

Ruby -- force method_missing *args to be hash?

I want to define a method_missing function for one of my classes, and I want to be able to pass in a hash as the argument list instead of an array. Like this:
MyClass::get_by_id {:id => id}
MyClass::get_by_id {:id => id, :filters => filters}
MyClass::get_by_id {:id => id, :filters => filters, :sort => sort}
As far as I can tell, the args list gets passed in as an array, so keys get dropped and there's no way to tell which arguments is which. Is there a way to force Ruby to treat the argument list in method_missing as a hash?
What issue are you having? This works for me:
class MyClass
def self.method_missing name, args
puts args.class
puts args.inspect
end
end
MyClass.foobar :id => 5, :filter => "bar"
# Hash
# {:id=>5, :filter=>"bar"}
Is this what you are looking for ?
class Foo
def self.method_missing(name,*args)
p args
p name
end
end
Foo.bar(1,2,3)
# >> [1, 2, 3]
# >> :bar
I'm answering my own question based on experimentation that I've done since asking. When using a hash splat on the arg list, you can pass in a hash like this:
MyClass::get_by_id(:id => id, :filters => filters)
And the argument list will look like this:
[
{
:id => id,
:filters => filters
}
]
Ruby places the key/value pairs into a single hash object at location args[0]. If you call the method like this:
MyClass::get_by_id(id, :filters => filters)
Your argument list will be this:
[
id,
{:filters => filters}
]
So basically, key/value pairs are merged together into a single hash and passed in order.

Specifying default value for Hash of hashes in Ruby [duplicate]

This question already has answers here:
Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])
(4 answers)
Closed 5 years ago.
Lets say I have a Hash called person whose key is name and value is a hash having keys 'age' and 'hobby'. An entry in the hash person would look like
=> person["some_guy"] = {:hobby => "biking", :age => 30}
How would I go about specifying the default for the hash 'person'? I tried the following
=> person.default = {:hobby => "none", :age => 20}
but it doesn't work.
EDIT:
I was setting one attribute and expecting others to be auto-populated. For eg.
=> person["dude"][:age] += 5
and this is what I was seeing
=> person["dude"]
=> {:hobby => "none", :age => 25}
which is fine. However, typing person at the prompt, I get an empty hash.
=> person
=> {}
However, what I was expecting was
=> person
=> {"dude" => {:hobby => "none", :age => 25}}
Why It May Not Work for You
You can't call Hash#default if an object doesn't have the method. You need to make sure that person.is_a? Hash before it will work. Are you sure you don't have an array instead?
It's also worth noting that a hash default doesn't populate anything; it's just the value that's returned when a key is missing. If you create a key, then you still need to populate the value.
Why It Works for Me
# Pass a default to the constructor method.
person = Hash.new({hobby: "", age: 0})
person[:foo]
=> {:hobby=>"", :age=>0}
# Assign a hash literal, and then call the #default method on it.
person = {}
person.default = {hobby: "", age: 0}
person[:foo]
=> {:hobby=>"", :age=>0}
What You Probably Want
Since hash defaults only apply to missing keys, not missing values, you need to take a different approach if you want to populate a hash of hashes. One approach is to pass a block to the hash constructor. For example:
person = Hash.new {|hash,key| hash[key]={hobby: nil, age:0}}
=> {}
person[:foo]
=> {:hobby=>nil, :age=>0}
person[:bar]
=> {:hobby=>nil, :age=>0}
person
=> {:foo=>{:hobby=>nil, :age=>0}, :bar=>{:hobby=>nil, :age=>0}}
You can specify a hash's default value on instantiation by passing the default value to the .new method:
person = Hash.new({ :hobby => '', :age => 0 })
You can use Hash#default_proc to initialise a hash with default values dynamically.
h = Hash.new
h.default_proc = proc do |hash, key|
hash[key] = Hash.new(0) unless hash.include? key
end
h[0][0] # => 0
h[1][0] # => 0
h[0][0] = 1
h[0][0] # => 1
h[1][0] # => 0

Resources