Extracting single values and keys from an array of hashes? [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 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

Related

Get value from hash with two keys [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 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

create nested hash from a list [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 5 years ago.
Improve this question
I have got a list l = [:foo, :bar, :baz] and want to assign a varible into a hash h ={} programmatically.
Hash should look like
{ foo: { bar: { baz: some_value } } }
Note: the keys are variables!
Question:
How can I do this?
You could use inject on the reversed list :
l = [:foo, :bar, :baz]
h = l.reverse.inject(:some_value) do |value, key|
{ key => value }
end
p h
# {:foo=>{:bar=>{:baz=>:some_value}}}
reverse is used in order to build the innermost hash first, and keep building the nested hash outwards.

Search an attribute for a substring [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 7 years ago.
Improve this question
I am trying to remove all whitespaces from an object's attribute that contains a given substring. For example, I have an object event and attributes: 4IP2, 3IP5, 2IP1. I would like to do the following:
event[4IP2].gsub(/\s+/, '')
in a generic manner, i.e.,
event[*IP*].gsub(/\s+/, '')
which should work for all attributes 4IP2, 3IP5, 2IP1. Appreciate any help.
Assuming, that event is a hash, here you go:
▶ event = { '4IP2' => 'a b c', '3GG5' => 'ffff f', '2IP1' => 'ggg ' }
▶ event.map { |k, v| [k, /IP/ =~ k ? v.delete(' ') : v] }.to_h
#⇒ { "2IP1" => "ggg", "3GG5" => "ffff f", "4IP2" => "abc" }
If you want to replace the attributes in-place:
event.each {|k,v| v.gsub!(/\s+/, '') if /IP/ =~ k}
Otherwise, to create a copy:
Hash[event.map {|k,v| [k, /IP/ =~ k ? v.gsub(/\s+/, '') : v]}]

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

Renaming an array created by Array.new [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
When creating a new array in IRB, I can use Array.new to get an empty, unassigned array.
Is there a way to reassign that new array? Can I turn, [] into a variable named my_new_array?
I know I can do this:
my_new_array = Array.new
Or I can do:
my_other_new_array = []
But what about reassigning Array.new?
I'm new to Ruby and I'm curious about this little nuance.
In IRB, the underscore _ method will give you the results of the last expression:
Array.new
# => []
my_new_array = _
# => []
Array.new(2, "foo")
# => ["foo", "foo"]
my_new_array = _
# => ["foo", "foo"]

Resources