From: Joe Gwozdecki Date: 2003-07-09T08:46:38+09:00 Subject: Re: Code Snippet: Array.shuffle "Brian Candler" wrote in message news:20030708224157.GA75351@uk.tiscali.com... > On Wed, Jul 09, 2003 at 06:05:28AM +0900, Joe Gwozdecki wrote: > > How come when I try out this code snippet in IRB, including > > entering the array, it does nothing? Am I missing > > something? > > I don't know. Here's an irb session log: > > $ irb > irb(main):001:0> class Array > irb(main):002:1> def shuffle > irb(main):003:2> each_index { |i| r = rand(length) > irb(main):004:3> self[i],self[r] = self[r],self[i] } > irb(main):005:2> end > irb(main):006:1> end > => nil > irb(main):007:0> a = [1,2,3,4,5,6,7,8,9,10] > => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > irb(main):008:0> a.shuffle > => [8, 7, 1, 4, 9, 10, 3, 2, 6, 5] > irb(main):009:0> a.shuffle > => [7, 10, 5, 9, 8, 1, 6, 3, 4, 2] > irb(main):010:0> > > What did you do differently? Note that if you miss a bracket or an 'end' > then irb may think you are in a continuation line; if the prompt ends with > an asterisk this is a pretty strong clue. > > irb(main):012:0> [ > irb(main):013:1* 1,2,3 > irb(main):014:1> ] > => [1, 2, 3] > > Regards, > > Brian. > Your coding works fine in IRB. The original poster wrote a = Array[1,2,3,4,5,6,7,8,9,10] which did not work. Get rid of "Array" and it works. Joe Gwozdecki Houston, Texas