Ask a Question related to Ruby, Design and Development.
-
Stefan Arentz #1
Code Snippet: Array.shuffle
Here is another small Ruby code snippet that extends the Array class. Use
this to randomize the order of all entries in an Array.
class Array
def shuffle
self.each_index { |i| r = rand(self.length - 1);
self[i],self[r] = self[r],self[i] }
end
end
Works like this:
a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.shuffle
==> [7, 10, 9, 6, 4, 8, 5, 3, 1, 2]
S.
Stefan Arentz Guest
-
Email Site-Wide Error message (with code snippet)
I would like to create a Site-Wide error message that is emailed to the administrator. I have a lot of the information I want to put in but I can... -
A code snippet: Controlled retry
I sometimes find myself retrying operations when in a networked situation (e.g., maybe server isn't up, just try again). I just wrote this little... -
#25247 [Fbk->NoF]: Shuffle() method doesn't work properly
ID: 25247 Updated by: sniper@php.net Reported By: jeroen at kesh dot nl -Status: Feedback +Status: ... -
#25247 [NEW]: Shuffle() method doesn't work properly
From: jeroen at kesh dot nl Operating system: Windows 2000 Server PHP version: 4.3.3 PHP Bug Type: Arrays related Bug... -
Ruby Code Snippet: Array.count
On Sat 05 Jul 2003 at 08:01:38 +0900, Stefan Arentz wrote: This is already available with Enumerable#find_all: a = (1..10).to_a puts... -
Jason Creighton #2
Re: Code Snippet: Array.shuffle
On 08 Jul 2003 03:01:30 +0200
Stefan Arentz <stefan.arentz@soze.com> wrote:
>
> Here is another small Ruby code snippet that extends the Array class. Use
> this to randomize the order of all entries in an Array.
>
> class Array
> def shuffle
> self.each_index { |i| r = rand(self.length - 1);
> self[i],self[r] = self[r],self[i] }
> end
> end
>
> Works like this:
>
> a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> a.shuffle
> ==> [7, 10, 9, 6, 4, 8, 5, 3, 1, 2]=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>> ary = (0..10).to_a=> [4, 6, 5, 0, 2, 3, 9, 7, 8, 1, 10]>> ary.sort { rand(3)-1 }
.....of course, this probably won't produce as random as result, but it's easier to implement.
Jason Creighton
Jason Creighton Guest
-
Joe Gwozdecki #3
Re: Code Snippet: Array.shuffle
"Brian Candler" <B.Candler@pobox.com> wrote in message
news:20030708063637.GA74975@uk.tiscali.com...wrote:> On Tue, Jul 08, 2003 at 10:21:42AM +0900, Stefan ArentzArray class. Use> >
> > Here is another small Ruby code snippet that extends theshould use>> > this to randomize the order of all entries in an Array.
> >
> > class Array
> > def shuffle
> > self.each_index { |i| r = rand(self.length - 1);
> > self[i],self[r] = self[r],self[i] }
> > end
> > end
> rand(x) gives a number between 0 and x-1, so I think youthis will work> rand(self.length) not rand(self.length-1). In particularwith> properly for arrays containing 1 element.
>
> You could also replace self.each_index and self.lengththe semicolon.> each_index and length, respectively. And you don't needHow come when I try out this code snippet in IRB, including>
> Regards,
>
> Brian.
>
entering the array, it does nothing? Am I missing
something?
Joe Gwozdecki
Houston, Texas
Joe Gwozdecki Guest
-
Brian Candler #4
Re: Code Snippet: Array.shuffle
On Wed, Jul 09, 2003 at 06:05:28AM +0900, Joe Gwozdecki wrote:
I don't know. Here's an irb session log:> How come when I try out this code snippet in IRB, including
> entering the array, it does nothing? Am I missing
> something?
$ 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.
Brian Candler Guest
-
Joe Gwozdecki #5
Re: Code Snippet: Array.shuffle
"Brian Candler" <B.Candler@pobox.com> wrote in message
news:20030708224157.GA75351@uk.tiscali.com...wrote:> On Wed, Jul 09, 2003 at 06:05:28AM +0900, Joe Gwozdeckiincluding> > How come when I try out this code snippet in IRB,bracket or an 'end'>> > 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 aprompt ends with> then irb may think you are in a continuation line; if theYour coding works fine in IRB. The original poster wrote a> 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.
>
= 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
Joe Gwozdecki Guest
-
Brian Candler #6
Re: Code Snippet: Array.shuffle
On Wed, Jul 09, 2003 at 08:46:38AM +0900, Joe Gwozdecki wrote:
It looks OK to me:> 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.
irb(main):001:0> a = Array[1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):002:0>
(this is 1.6.8)
Brian Candler Guest
-
Joe Gwozdecki #7
Re: Code Snippet: Array.shuffle
"Brian Candler" <B.Candler@pobox.com> wrote in message
news:20030709073944.GA75875@uk.tiscali.com...wrote:> On Wed, Jul 09, 2003 at 08:46:38AM +0900, Joe Gwozdeckiwrote a> > Your coding works fine in IRB. The original posterrid> > = Array[1,2,3,4,5,6,7,8,9,10] which did not work. GetI got the error from -- a = array[1,2,3,4,5]>> > of "Array" and it works.
> It looks OK to me:
>
> irb(main):001:0> a = Array[1,2,3,4,5]
> => [1, 2, 3, 4, 5]
> irb(main):002:0>
>
> (this is 1.6.8)
>
IRB will give a consistent error.
You are right. a = Array[1,2,3,4,5] does
work just fine. Also a = [1,2,3,4,5] does too
and is probably cleaner.
Joe Gwozdecki
Houston, Texas
Joe Gwozdecki Guest
-
Brian Candler #8
Re: Code Snippet: Array.shuffle
On Wed, Jul 09, 2003 at 08:50:59PM +0900, Joe Gwozdecki wrote:
Certainly. Ruby is case-sensitive. You need to call method [] of 'Array',>> > It looks OK to me:
> >
> > irb(main):001:0> a = Array[1,2,3,4,5]
> > => [1, 2, 3, 4, 5]
> > irb(main):002:0>
> >
> > (this is 1.6.8)
> >
> I got the error from -- a = array[1,2,3,4,5]
> IRB will give a consistent error.
not 'array'
Cheers,
Brian.
Brian Candler Guest
-
Mauricio Fernández #9
Re: Code Snippet: Array.shuffle
On Wed, Jul 09, 2003 at 09:35:22PM +0900, Brian Candler wrote:
It's not Array.[], but Kernel#Array.> On Wed, Jul 09, 2003 at 08:50:59PM +0900, Joe Gwozdecki wrote:>> >> > > It looks OK to me:
> > >
> > > irb(main):001:0> a = Array[1,2,3,4,5]
> > > => [1, 2, 3, 4, 5]
> > > irb(main):002:0>
> > >
> > > (this is 1.6.8)
> > >
> > I got the error from -- a = array[1,2,3,4,5]
> > IRB will give a consistent error.
> Certainly. Ruby is case-sensitive. You need to call method [] of 'Array',
> not 'array'
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Software is like sex; it's better when it's free.
-- Linus Torvalds
Mauricio Fernández Guest
-
Brian Candler #10
Re: Code Snippet: Array.shuffle
On Wed, Jul 09, 2003 at 09:45:33PM +0900, Mauricio Fernández wrote:
You sure? This is Kernel#Array:>> >> > > I got the error from -- a = array[1,2,3,4,5]
> > > IRB will give a consistent error.
> > Certainly. Ruby is case-sensitive. You need to call method [] of 'Array',
> > not 'array'
> It's not Array.[], but Kernel#Array.
Array(5) #>> [5]
Array(5,6) #>> ArgumentError: wrong # of arguments(2 for 1)
I believe that foo[bar] is a shortcut for
foo.send(:[], bar)
And therefore Array[1,2,3,4,5] is
Array.send(:[], 1, 2, 3, 4, 5)
#=> [1, 2, 3, 4, 5]
or equivalently
Array.[] (1,2,3,4,5)
#=> [1, 2, 3, 4, 5]
i.e. this is a class (singleton) method of Array
Regards,
Brian.
Brian Candler Guest
-
Brian Candler #11
Re: Code Snippet: Array.shuffle
On Thu, Jul 10, 2003 at 12:16:59AM +0900, Mauricio Fernández wrote:
Ah, I see: foo[bar] => foo.[] (bar)> Anyway, this is yet another case of significant spaces in Ruby:
>> ?> def [](*a)> >> class << Array
> >> alias_method :_old_, :[];> => nil> >> puts "Array.[]"
> >> _old_(*a)
> >> end
> >> end> Array.[]> >> Array[1,2,3]
> => [1, 2, 3]> => [1, 2, 3]> >> Array [1,2,3]
foo [bar] => foo( [bar] )
Yuk. This exists in 1.6.8. I guess it mirrors Matz's change introduced for
1.8:
foo(a+b).c => ( foo(a+b) ).c
foo (a+b).c => foo( (a+b).c )
As for the semicolon, it seems that irb does not play nicely with unusual
symbols:
$ irb
irb(main):001:0> :ok
=> :ok
irb(main):002:0> :[]
irb(main):003:0* puts "oops"
oops
=> nil
irb(main):004:0>
Cheers,
Brian.
Brian Candler Guest



Reply With Quote

