Hi,
first: you don't need the 'to_a'
second:
···
---------------------------------------
(0..5).inject{|x,y| print y, ' '}
puts
(1..5).inject(0){|x,y| print y, ' '}
puts
---------------------------------------
output:
1 2 3 4 5
1 2 3 4 5
If you provide no parameter to inject it will give
you the first item of the enumerable as x.
cheers
Simon
-----Original Message-----
From: Paul Novak [mailto:novakps@gmail.com]
Sent: Monday, January 09, 2006 3:04 PM
To: ruby-talk ML
Subject: Re: [QUIZ.SOLUTION] Dice Roller (#61) We don't need
no steenking leexer/parsersNo, the mis-understanding is all mine. You are right, otherwise it
will include an extra roll. It should be:
(1..self).to_a.inject(0){|x,y| x + rand(sides)}+selfFurther proof of the value of good unit tests. (Since (1..1).to_a
returns a single-member array, you need to provide inject with an
initial value to make it work.)Regards,
Paul.
On 1/9/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
> } # roll the dice
> } (0..self).to_a.inject{|x,y| x + rand(sides)}+self
>
> I think I may be misunderstanding something here. You use
0..self, when I
> would think it would have to be either 0...self or 1..self
to get the right
> number of rolls. Am I off? In my solution I used 1..self in
much the same
> way.