From: Roelof Wobben Date: 2012-09-25T21:56:44+09:00 Subject: Re: inject problem --_e7951a01-051f-4b04-bee6-7908896d23f5_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable All thanks for the explanation. I have one question about the solution of Prasadhnc C When I do this :=20 array.each do |x| count.inject({}){|x=2Cy| x[y] +=3D 1 rescue x[y] =3D 1=3B x} end where x =3D array [ 1=2C 2 =2C 1 =2C 1] Then I don't get the counted answers. Roelof > Date: Tue=2C 25 Sep 2012 18:48:29 +0900 > From: jgabrielygalan@gmail.com > Subject: Re: inject problem > To: ruby-talk@ruby-lang.org >=20 > On Tue=2C Sep 25=2C 2012 at 11:31 AM=2C Jan E. wro= te: > > Hi=2C > > > > Using "inject" really isn't a good choice here. If at all=2C I'd use > > "each_with_object" to build up the hash. >=20 > My preferred solution: >=20 > count =3D [1=2C2=2C1=2C1] > h =3D Hash.new(0) > count.each {|el| h[el] +=3D 1} > p h #=3D> {1=3D>3=2C 2=3D>1} >=20 >=20 > Jesus. >=20 = --_e7951a01-051f-4b04-bee6-7908896d23f5_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
All thanks for the explanation.

I have one question about the soluti= on of Prasadhnc C

When I do this :

array.each do |x|<= br> =3B =3B =3B =3B count.inject({}){|x=2Cy| x[y] +=3D 1 re= scue x[y] =3D 1=3B x}
end

where x =3D array [ 1=2C 2 =2C 1 =2C 1]=

Then I don't get the counted answers.

Roelof


>=3B Date: Tue=2C 25 Sep 2012 18:4= 8:29 +0900
>=3B From: jgabrielygalan@gmail.com
>=3B Subject: Re: = inject problem
>=3B To: ruby-talk@ruby-lang.org
>=3B
>=3B O= n Tue=2C Sep 25=2C 2012 at 11:31 AM=2C Jan E. <=3Blists@ruby-forum.com>= =3B wrote:
>=3B >=3B Hi=2C
>=3B >=3B
>=3B >=3B Using "= inject" really isn't a good choice here. If at all=2C I'd use
>=3B >= =3B "each_with_object" to build up the hash.
>=3B
>=3B My prefer= red solution:
>=3B
>=3B count =3D [1=2C2=2C1=2C1]
>=3B h = =3D Hash.new(0)
>=3B count.each {|el| h[el] +=3D 1}
>=3B p h #=3D= >=3B {1=3D>=3B3=2C 2=3D>=3B1}
>=3B
>=3B
>=3B Jesus.<= br>>=3B
= --_e7951a01-051f-4b04-bee6-7908896d23f5_--