sort_by{rand} doesn't shuffle array

Discussion in 'Ruby' started by Pat Maddox, Jan 3, 2006.

  1. Pat Maddox

    Pat Maddox Guest

    I did some searching on how to shuffle an array, and found
    sort_by{rand}. However, if I call it, it doesn't make a change to my
    array at all. I've got a class like this
    class MyClass
    def initialize
    # Populate the array
    @my_array =3D Array.new
    ...
    end

    def shuffle!
    @my_array.sort_by{rand}
    end
    end

    If I call shuffle! from my code, the array's elements are still in the
    order that they were created. Why are they not being shuffled like I
    want?
     
    Pat Maddox, Jan 3, 2006
    #1
    1. Advertising

  2. On Tue, Jan 03, 2006 at 07:03:42PM +0900, Pat Maddox wrote:
    > def shuffle!
    > @my_array.sort_by{rand}

    @my_array = @my_array.sort_by{rand}
    > end
    > end


    --
    Mauricio Fernandez
     
    Mauricio Fernandez, Jan 3, 2006
    #2
    1. Advertising

  3. Mauricio Fernandez wrote:

    >On Tue, Jan 03, 2006 at 07:03:42PM +0900, Pat Maddox wrote:
    >
    >
    >> def shuffle!
    >> @my_array.sort_by{rand}
    >>
    >>

    > @my_array = @my_array.sort_by{rand}
    >
    >

    or @my_array.sort_by! { rand }

    >> end
    >>end
    >>
    >>
     
    Devin Mullins, Jan 3, 2006
    #3
  4. > or @my_array.sort_by! { rand }
    Except that sort_by! does not exist ;-) (at least not in 1.8.4)
    --
    Sylvain
     
    Sylvain Joyeux, Jan 3, 2006
    #4
  5. On Tue, Jan 03, 2006 at 08:54:33PM +0900, Devin Mullins wrote:
    > Mauricio Fernandez wrote:
    >
    > >On Tue, Jan 03, 2006 at 07:03:42PM +0900, Pat Maddox wrote:
    > >
    > >
    > >> def shuffle!
    > >> @my_array.sort_by{rand}
    > >>
    > >>

    > > @my_array = @my_array.sort_by{rand}
    > >
    > >

    > or @my_array.sort_by! { rand }


    RUBY_RELEASE_DATE # => "2005-12-24"
    RUBY_VERSION # => "1.8.4"
    %w[a b c d].sort_by!{rand} # =>
    # ~> -:3: undefined method `sort_by!' for ["a", "b", "c", "d"]:Array (NoMethodError)

    --
    Mauricio Fernandez
     
    Mauricio Fernandez, Jan 3, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Dershowitz

    sort_by { rand } not working

    Mike Dershowitz, Aug 26, 2007, in forum: Ruby
    Replies:
    20
    Views:
    289
    Dan Zwell
    Aug 27, 2007
  2. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    236
    Morton Goldberg
    Sep 16, 2007
  3. Jeff Moore

    Array.shuffle/Array.shuffle!

    Jeff Moore, Aug 24, 2008, in forum: Ruby
    Replies:
    6
    Views:
    156
    Alexei Broner
    Oct 9, 2008
  4. Tom Eaton
    Replies:
    2
    Views:
    98
    Robert Klemme
    Feb 9, 2010
  5. Tuxedo
    Replies:
    8
    Views:
    134
    Tuxedo
    May 7, 2007
Loading...

Share This Page