Stephen Ball
8/29/2007 2:05:00 PM
On 8/29/07, Mark Ransom <mark.ransom@comcast.net> wrote:
> Hi,
>
> I'm a novice programmer who is just starting out in Ruby. I've been
> playing around with arrays and have run into a problem:
>
> This works:
>
> array = [3,2,1]
> puts array.sort
>
> =>123
>
> BUT this doesn't (error attached):
>
> nums = Array.new
>
> numplays = 5
>
> numplays.times do
>
> for values in 1..5
> ball = rand(56)
> redo if ball == 0 || nums.include?(ball)
> nums [values] = ball
> end
> puts nums.sort
> end
>
> Can anyone shed light on this newby?
>
The problem is that you start your array index at 1, not 0.
Orig: for values in 1..5
Mod: for values in 0..4
If you want to see what's happening in your code, add a 'puts
nums.inspect' right after you assign the ball value to nums.
-- Stephen