Get value from hash with two keys [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a hash whose keys are an array of two elements like this:
logs_data =
{
[ 143184, 11467 ] => {
:finished => true,
:created_at => 2017-11-09 09:38:11 UTC
},
[ 143184, 11471 ] => {
:finished => true,
:created_at => 2017-12-20 07:21:02 UTC
}
}
I don't know how to get the value from a key. I tried this way:
logs_data[143184, 11467]
#=> ArgumentError: wrong number of arguments (given 2, expected 1)
but it failed.

2.4.2 :027 > logs_data.keys
=> [[143184, 11467], [143184, 11471]]
your key is [143184, 11467] so you have to do hash[key] like following
2.4.2 :028 > logs_data[[143184, 11467]]
=> {:finished=>true, :created_at=> 2017-11-09 09:38:11 UTC}

You need two sets of brackets logs_data[[ key ]]
In ruby you can also do logs_data.keys or logs_data.values

Related

Ruby Data Types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Which two of these three expressions are equal? Why?
{ "city" => "Miami", "state" => "Florida" }
{ :city => "Miami", :state => "Florida" }
{ city: "Miami", state: "Florida" }
There is a great discussion on using a Ruby :symbol vs a String in another question here.
And here's a nice discussion about the difference between the fat arrow => syntax vs colons : in Ruby.
You can quickly check that the two hashes using :symbols are equivalent to each other, which are both different from the hash using strings:
a = {"city" => "Miami", "state" => "Florida"}
b = {:city => "Miami", :state => "Florida"}
c = {city: "Miami", state: "Florida"}
a == b
=> false
a == c
=> false
b == c
=> true

Find highest values from associated array in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have complex hash which looks like this
#hash = {1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[], 10=>[], 11=>[], 12=>[[{"value"=>1.58, "title"=>"sun", "quantity" => 2}], [{"value"=>1.99, "title"=>"sophia", "quantity" => 5}], [{"value"=>6.30, "title"=>"roam", "quantity" => 15}], [{"value"=>3.981, "title"=>"jia, "quantity" => 4"}], 13 => [], 14 => [], 15 => []}
now I want to extract highest value along with associated title and quantity. index would 15 all the time.
for example the output should be
#hash = { value => 6.30, title => "roam", quantity => 15 }
I was searhcing some found this but did not make it work
reference link Ref
help appreciated thanks
If you're not interested in the element's index, you could flatten the values and find the maximum one:
#hash = {
1=>[], 2=>[], 3=>[], 4=>[], 5=>[], 6=>[], 7=>[], 8=>[], 9=>[],
10=>[], 11=>[], 12=>[
[{"value"=>1.58, "title"=>"sun", "quantity" => 2}],
[{"value"=>1.99, "title"=>"sophia", "quantity" => 5}],
[{"value"=>6.30, "title"=>"roam", "quantity" => 15}],
[{"value"=>3.981, "title"=>"jia", "quantity" => "4"}]
],
13 => [], 14 => [], 15 => []
}
#hash.values.flatten.max_by { |h| h["value"] }
#=> {"value"=>6.3, "title"=>"roam", "quantity"=>15}

How to iterate through this array within an array to see if all values are equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is my sample code
:key1 => "a"
:key2 => "b"
:key3 => "c"
array1 = [[:key1, :key1, :key1],[:key1, :key2, :key3],[:key2, :key2, :key1]]
array1.each { |x| if x.sym_tos == "a"
puts "All match!"
else
puts "no match"
end
}
Yet when I run it, I get the following error code:
undefined method `sym_to_s' for [:R1C1, :R1C2, :R1C3]:Array (NoMethodError)
You probably wanted to say
if x.uniq.length == 1

Extracting single values and keys from an array of hashes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Right now, I have a structure like this:
[
{ asin: "B000O3GCFU", name: "Thermos...", price: "$10.19" },
{ asin: "B0025Y6742", name: "Thermos...", price: "$12.19" }
# ...
]
So, an array of hashes.
How can I extract single keys and values of each of the hashes? Like:
[
{ asin: "B000O3GCFU" },
{ asin: "B0025Y6742" }
# ...
]
You can use map and create new hashes containing only "asin" on the fly:
a.map {|h| {:asin => h[:asin]}}
product_hash[:product].map do |product|
product.slice(:asin) # if you have activesupport
product.select { |key, val| key == :asin } # if you don't
end

prime number test in Ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array a = [1,2,3,4,5]. I want to test which of the numbers are prime and wanted to produce the output {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}.
Any one liner will be appreciated.
I posted this as a comment.
require 'prime'
a = (1..5).to_a
Hash[a.map{ |x| [x, x.prime?] }]
=> {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}
The below will work for you,using Prime#prime?:
require 'prime'
a = [1,2,3,4,5]
Hash[a.zip(a.map(&Prime.method(:prime?)))]
# => {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}
An alternate solution:
a = [1,2,3,4,5]
results = {}
a.each {|i| results[i] = (1..i).map{|x| i/x.to_f % 1 == 0}.count(true) == 2}
puts results.inspect #=> {1 => false, 2 => true, 3 => true, 4 => false, 5 => true}

Resources