Hi, On Wed, Aug 29, 2007 at 10:39:21PM +0900, Mark Ransom 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 puts nums.join(',') > end > puts nums.sort > end > > Can anyone shed light on this newby? So what you are attempting to do is generate 25 random numbers between 1 and 55, with no duplicates? > http://www.ruby-forum.com/attachment/183/array_sort.JPG The problem you are having is that when you get a number, you insert it into the array at slots 1,2,3,4 and 5. But your array is actually a 0 based index. The first slot is nums[0]. That means when you do your sort you are sorting for instance [nil,2,32,1,44,26] which is an array of size 6, and nil doesn't compare with an integer via the <=> operator. The simpliest way to solve your issue is to change: for values in 1..5 to 5.times do |values| If you want to change it up even more and not have to 'redo' then you might change nums to a Hash and use its keys to hold the 'ball' values nums = {} while nums.size < 5 do ball = rand(55) + 1 # gives a value 1..55 nums[ball] = ball # put ball into the hash end puts num.keys.sort.join(',') It appears the basic problem you are doing is to choose 5 distinct numbers from 1..55 N times. You may want to look at this ruby quiz for interesting items: http://www.rubyquiz.com/quiz39.html enjoy, -jeremy -- ======================================================================== Jeremy Hinegardner jeremy / hinegardner.org