Rafa_F
June 28, 2013, 7:21pm
1
array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.45} # index[2]
{:id=>4, :price =>0.55} # index[3]
I am beginning to adventure in Ruby and I need to find in this array of
hashes the id with the value of 3 and get its price. What would be the
easiest way to do it?
Thanks
rodusa
June 28, 2013, 7:36pm
2
Hi Rodrigo,
Check out Enumerable#find.
With a hash it’ll look something like this:
hsh.find { |key, value| value[:yourkey] == yourvalue }
That should get you on the right track.
Cheers,
Jason
rodusa
June 28, 2013, 8:01pm
4
Thanks
I tried your code but I keep getting nil, but I know for sure that the
value “3” is in there.
sql = "SELECT p.id, s.price FROM services s inner join products as p
ON s.product_id = p.id where s.shop_id = 1"
r = ActiveRecord::Base.connection.select_all(sql)
@array = r.find {|k,v| k[:id] == “3”}
if I change it to
@array = r.find {|k,v| v[:id] == “3”}
I get
undefined method `[]’ for nil:NilClass
Rod
rodusa
June 28, 2013, 8:40pm
5
Joel, I fixed it, but still get the error. Look:
results = @selectedProductsArray.find {|k,v| v[:id] == 3 }
NoMethodError in ServicesController#list
undefined method `[]’ for nil:NilClass
rodusa
June 28, 2013, 8:49pm
6
I know it is an array, but inside the array there are two hashe objects
and
this is driving me nuts as to using the code above. Look the picture
attached:
rodusa
June 28, 2013, 8:43pm
7
Rodrigo L. wrote in post #1113851:
I fixed it, but still get the error. Look:
results = @selectedProductsArray.find {|k,v| v[:id] == 3 }
NoMethodError in ServicesController#list
undefined method `[]’ for nil:NilClass
An array is a sequence of elements, not key-value pairs:
results = @selectedProductsArray.find {|e| e[:id] == 3 }
rodusa
June 28, 2013, 9:27pm
8
On Fri, Jun 28, 2013 at 7:21 PM, Rodrigo L.
[email protected] wrote:
array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.25} # index[2]
I am beginning to adventure in Ruby and I need to find in this array of
hashes the id with the value of 3 and get its price. What would be the
easiest way to do it?
There is no Array:
ruby <<XXXX
array = {:id=>1, :price =>0.25} # index[0]
{:id=>2, :price =>0.35} # index[1]
{:id=>3, :price =>0.25} # index[2]
p array
XXXX
{:id=>1, :price=>0.25}
Cheers
robert
rodusa
June 28, 2013, 8:57pm
9
How about this?
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }
rodusa
June 29, 2013, 1:58am
10
I am sorry, I did not understand what you tried to convey. To me it is
clear that is an array of hashes. I just don’t get your example…
rodusa
June 29, 2013, 3:36am
11
On 6/28/2013 5:58 PM, Rodrigo L. wrote:
I am sorry, I did not understand what you tried to convey. To me it is
clear that is an array of hashes. I just don’t get your example…
He means your example code did not produce and array. Consider the
following irb session:
irb(main):003:0> array = {:id => 1, :price => 0.25}
=> {:id=>1, :price=>0.25}
irb(main):005:0> {:id => 2, :price => 0.35}
=> {:id=>2, :price=>0.35}
irb(main):006:0> {:id => 3, :price => 0.25}
=> {:id=>3, :price=>0.25}
irb(main):007:0> array
=> {:id=>1, :price=>0.25}
irb(main):008:0> array.class
=> Hash
That’s because you need to put your array in a comma separated list of
values inside []:
irb(main):026:0> array = [{ :id => 1, :price => 0.25 },
irb(main):027:1* { :id => 2, :price => 0.35 },
irb(main):028:1* { :id => 3, :price => 0.25 }]
=> [{:id=>1, :price=>0.25}, {:id=>2, :price=>0.35}, {:id=>3,
:price=>0.25}]
Now that we have an array, we can find the element with the id of 3 like
so:
irb(main):029:0> item = array.find {|hash| hash[:id] == 3 }
=> {:id=>3, :price=>0.25}
and get it’s price:
irb(main):030:0> item[:price]
=> 0.25
Hopefully that helps.
Walton
rodusa
June 29, 2013, 1:56am
12
Joel,
I tried your suggestion and I get “nil” as a result. It is a just weird
behavior!!!
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }
results = nil
rodusa
June 29, 2013, 3:43am
13
Walton,
I got it to work now! This was my fault.
I did not post the the sample array correctly. It should be like this:
array = {:id=>1, :price =>0.25}, # index[0]
:id=>2, :price =>0.35}, # index[1]
:id=>3, :price =>0.45}, # index[2]
:id=>4, :price =>0.55} # index[3]
thank you for all you guys
rodusa
June 29, 2013, 11:20am
14
Am 29.06.2013 03:43, schrieb Rodrigo L.:
thank you for all you guys
No, that’s still not an array of hashes, that’s a couple of
syntax errors. You are still forgetting ‘[’, ‘]’ and some ‘{’ …
This is an array of hashes:
array = [
{:id => 1, :price => 0.25}, # index[0]
{:id => 2, :price => 0.35}, # index[1]
{:id => 3, :price => 0.45}, # index[2]
{:id => 4, :price => 0.55} # index[3]
]
rodusa
June 29, 2013, 6:59pm
15
On Sat, Jun 29, 2013 at 6:05 PM, Rodrigo L.
[email protected] wrote:
Thanks,
Just to conclude my approach.
hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }
This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }
Nobody will be able to understand your conclusion in absence of the real
code you used to create the Array - if you created an array.
Good luck!
robert
rodusa
June 29, 2013, 6:05pm
16
Thanks,
Just to conclude my approach.
hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }
This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }
Rod
rodusa
June 29, 2013, 8:18pm
17
It’s worrying that so many people who put their code problems on here
seem to manually type what they’re looking at, instead of using
copy-paste.
rodusa
June 29, 2013, 9:01pm
18
Am 29.06.2013 18:05, schrieb Rodrigo L.:
Thanks,
Just to conclude my approach.
hash symbol did not work with select_all method
results = @selectedProductsArray.find {|hash| hash[:id] == 3 }
This worked:
results = @selectedProductsArray.find {|hash| hash[“id”] == 3 }
Of course, when your actual hashes use strings as keys,
and not symbols like in the example you gave earlier.
Regards,
Marcus
rodusa
June 29, 2013, 10:16pm
19
On Sat, Jun 29, 2013 at 8:18 PM, Joel P. [email protected]
wrote:
It’s worrying that so many people who put their code problems on here
seem to manually type what they’re looking at, instead of using
copy-paste.
Well, maybe someone told them that c&p is bad in programming…
robert
rodusa
June 30, 2013, 12:16am
20
Taking DRY to the extreme…
Sent from my phone, so excuse the typos.