From: Jason Stewart Date: 2013-06-29T02:36:16+09:00 Subject: Re: Find elment in array of hashes --20cf307f356ecdab6304e03a5028 Content-Type: text/plain; charset=ISO-8859-1 Hi Rodrigo, Check out Enumerable#find. http://ruby-doc.org/core-2.0/Enumerable.html#method-i-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 On Fri, Jun 28, 2013 at 1:21 PM, Rodrigo Lueneberg 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? > > Thanks > > -- > Posted via http://www.ruby-forum.com/. > > --20cf307f356ecdab6304e03a5028 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi Rodrigo,

Check out Enumerable#= find.=A0
http://ruby-doc.org/core-2.0/Enumerable.html#method-i-= find

With a hash it'll look something = like this:
hsh.find { |key, value| value[:yourkey] =3D=3D y= ourvalue }

That should get you on the = right track.

Cheers,
Jason



On Fri, Jun 28, 2013 at 1:21 PM, Rodrigo Lueneberg = <lists@ruby-fo= rum.com> wrote:
array =3D {:id=3D>1, :price =3D>0.25} = # index[0]
=A0 {:id=3D>2, :price =3D>0.35} # index[1]
=A0 {:id=3D>3, :price =3D>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?

Thanks

--
Posted via http://= www.ruby-forum.com/.


--20cf307f356ecdab6304e03a5028--