From: Roelof Wobben Date: 2012-09-26T04:43:47+09:00 Subject: Re: inject problem --_34967ada-c7e1-4d4b-9dca-07128accc874_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thanks but I won't solve my problem. When I change the method to this : def score(dice) total =3D 0=20 h =3D Hash.new(0) dice.each {|el| h[el] +=3D 1}=20 dice.each {|number=2C count| if number =3D=3D 1 and count =3D=3D 6 then total =3D 2000 end if number !=3D 1 and count =3D=3D 3 then total =3D total + 100 * count e= nd if number !=3D 1 and count =3D=3D 6 then total =3D 600 end if number =3D=3D 1 and (count > 3) then total =3D total + ( 100 + ( coun= t - 3)) end if number =3D=3D 1 and (count < 3) then total =3D total + 100 * count = end if number =3D=3D 5 and (count > 3) then total =3D total + ( 50 * (count = -3)) end if number =3D=3D 5 and (count < 3) then total =3D total + 50 * count en= d } =20 return total=20 end then I see this error message :=20 The answers you seek... undefined method `>' for nil:NilClass Please meditate on the following code: ./about_scoring_project.rb:43:in `score' ./about_scoring_project.rb:37:in `each' ./about_scoring_project.rb:37:in `score' ./about_scoring_project.rb:58:in `test_score_of_a_single_roll_of_5_is_50' /home/roelof/koans/edgecase.rb:398:in `send' /home/roelof/koans/edgecase.rb:398:in `meditate' /home/roelof/koans/edgecase.rb:470:in `walk' /home/roelof/koans/edgecase.rb:481:in `each_step' /home/roelof/koans/edgecase.rb:479:in `each' /home/roelof/koans/edgecase.rb:479:in `each_step' path_to_enlightenment.rb:38:in `each_with_index' /home/roelof/koans/edgecase.rb:478:in `each' /home/roelof/koans/edgecase.rb:478:in `each_with_index' /home/roelof/koans/edgecase.rb:478:in `each_step' /home/roelof/koans/edgecase.rb:476:in `catch' /home/roelof/koans/edgecase.rb:476:in `each_step' /home/roelof/koans/edgecase.rb:469:in `walk' /home/roelof/koans/edgecase.rb:491 path_to_enlightenment.rb:38 > Date: Wed=2C 26 Sep 2012 04:02:29 +0900 > From: lists@ruby-forum.com > Subject: Re: inject problem > To: ruby-talk@ruby-lang.org >=20 > Roelof Wobben wrote in post #1077510: > > The only problem I have to solve is when dice is a empty array. > > you now get [] as answer but the answer has to be 0. >=20 > Yeah=2C you seem to have forgot "return total" at the end of the method=20 > (you can leave out the "return"=2C of course). Otherwise the return value= =20 > is the result of "each" (which is an array). >=20 > --=20 > Posted via http://www.ruby-forum.com/. >=20 = --_34967ada-c7e1-4d4b-9dca-07128accc874_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Thanks but I won't solve my problem.

When I change the method to thi= s :

def score(dice)
 =3B total =3D 0
 =3B h =3D Hash.= new(0)
 =3B dice.each {|el| h[el] +=3D 1}

 =3Bdice.each = {|number=2C count|
 =3B =3B if number =3D=3D 1 and count =3D=3D = 6 then total =3D =3B 2000 end
 =3B =3B if number !=3D 1 and = count =3D=3D 3 then total =3D total + 100 * count end
 =3B =3B i= f number !=3D 1 and count =3D=3D 6 then total =3D =3B 600 end
 = =3B =3B if number =3D=3D 1 and (count >=3B 3) then total =3D total + = ( 100 + ( count - 3)) end
 =3B =3B if number =3D=3D 1 and (count= <=3B 3) then total =3D total =3B =3B + 100 * count end
 = =3B =3B if number =3D=3D 5 and (count >=3B 3) then total =3D total + = ( 50 * (count -3)) end
 =3B =3B if number =3D=3D 5 and (count &l= t=3B 3) then total =3D total + =3B 50 * count end
 =3B =3B&n= bsp=3B }
 =3B
 =3B =3B return total

end

th= en I see this error message :

The answers you seek...
 =3B u= ndefined method `>=3B' for nil:NilClass

Please meditate on the fol= lowing code:
 =3B ./about_scoring_project.rb:43:in `score'
 = =3B ./about_scoring_project.rb:37:in `each'
 =3B ./about_scoring_pro= ject.rb:37:in `score'
 =3B ./about_scoring_project.rb:58:in `test_sc= ore_of_a_single_roll_of_5_is_50'
 =3B /home/roelof/koans/edgecase.rb= :398:in `send'
 =3B /home/roelof/koans/edgecase.rb:398:in `meditate'=
 =3B /home/roelof/koans/edgecase.rb:470:in `walk'
 =3B /home= /roelof/koans/edgecase.rb:481:in `each_step'
 =3B /home/roelof/koans= /edgecase.rb:479:in `each'
 =3B /home/roelof/koans/edgecase.rb:479:i= n `each_step'
 =3B path_to_enlightenment.rb:38:in `each_with_index'<= br> =3B /home/roelof/koans/edgecase.rb:478:in `each'
 =3B /home/= roelof/koans/edgecase.rb:478:in `each_with_index'
 =3B /home/roelof/= koans/edgecase.rb:478:in `each_step'
 =3B /home/roelof/koans/edgecas= e.rb:476:in `catch'
 =3B /home/roelof/koans/edgecase.rb:476:in `each= _step'
 =3B /home/roelof/koans/edgecase.rb:469:in `walk'
 =3B= /home/roelof/koans/edgecase.rb:491
 =3B path_to_enlightenment.rb:38=




>=3B Date:= Wed=2C 26 Sep 2012 04:02:29 +0900
>=3B From: lists@ruby-forum.com
= >=3B Subject: Re: inject problem
>=3B To: ruby-talk@ruby-lang.org>=3B
>=3B Roelof Wobben wrote in post #1077510:
>=3B >=3B T= he only problem I have to solve is when dice is a empty array.
>=3B &g= t=3B you now get [] as answer but the answer has to be 0.
>=3B
>= =3B Yeah=2C you seem to have forgot "return total" at the end of the method=
>=3B (you can leave out the "return"=2C of course). Otherwise the re= turn value
>=3B is the result of "each" (which is an array).
>= =3B
>=3B --
>=3B Posted via http://www.ruby-forum.com/.
>= =3B
= --_34967ada-c7e1-4d4b-9dca-07128accc874_--