Shuffle Array.

Discussion in 'Ruby' started by bjorn2k@hotmail.com, Jun 12, 2006.

  1. Guest

    Hi,

    I'm trying to implement a shuffle on an Array. I created a derived
    class of Array.

    -- Ruby Code --
    class CardStack<Array
    def my_shuffle!
    size.times do
    push slice! rand(size)
    end
    self
    end
    end

    deck = CardStack.new

    deck = [1,2,3,4,5,6]

    deck.my_shuffle!
    -- Ruby Code --

    I get the following error: undefined method `my_shuffle!' for [1, 2,
    3, 4, 5, 6]:Array (NoMethodError). What am i doing wrong.

    --
    Bjorn
     
    , Jun 12, 2006
    #1
    1. Advertising

  2. ekofoed Guest

    Hi,

    The error is obvious:

    > deck = CardStack.new
    >
    > deck = [1,2,3,4,5,6]


    The second "deck=" assigns deck to a new array. Your CardStack is gone.

    Why not extend the Array class with your my_shuffle! definition
    instead?

    If you want a class of its own, you will need some way of assigning,
    either through initialize or by overloading "=".

    regards

    -erik

    wrote:
    > Hi,
    >
    > I'm trying to implement a shuffle on an Array. I created a derived
    > class of Array.
    >
    > -- Ruby Code --
    > class CardStack<Array
    > def my_shuffle!
    > size.times do
    > push slice! rand(size)
    > end
    > self
    > end
    > end
    >
    > deck = CardStack.new
    >
    > deck = [1,2,3,4,5,6]
    >
    > deck.my_shuffle!
    > -- Ruby Code --
    >
    > I get the following error: undefined method `my_shuffle!' for [1, 2,
    > 3, 4, 5, 6]:Array (NoMethodError). What am i doing wrong.
    >
    > --
    > Bjorn
     
    ekofoed, Jun 12, 2006
    #2
    1. Advertising

  3. wrote:
    > class CardStack<Array
    > def my_shuffle!
    > size.times do
    > push slice! rand(size)
    > end
    > self
    > end
    > end
    >
    > deck = CardStack.new
    >
    > deck = [1,2,3,4,5,6]


    Unlike C++, = is not a method-based operator in ruby; it's more of a
    'keyword operator' that binds a variable to an object. So here what you
    are doing is creating a new CardStack, binding deck to it, then creating
    a new Array and binding deck to *that* instead. (The new CardStack is
    lost, and will be garbage collected). What you need to do, instead, is

    deck = CardStack.new([1,2,3,4,5,6])

    where CardStack inherits the copy constructor from Array and does indeed
    initialise itself properly.

    martin
     
    Martin DeMello, Jun 12, 2006
    #3
  4. ekofoed <> wrote:
    >
    > If you want a class of its own, you will need some way of assigning,
    > either through initialize or by overloading "=".


    You can't overload = in Ruby. This is because a variable is simply a
    name that the = operator binds to the object on its lhs (and is dereferenced
    when used on the rhs of an expression). Variables themselves are not
    objects, nor do they carry any type information, they are just
    transparent aliases to actual ruby objects.

    martin
     
    Martin DeMello, Jun 12, 2006
    #4
  5. wrote:
    > Hi,
    >
    > I'm trying to implement a shuffle on an Array. I created a derived
    > class of Array.
    >
    > -- Ruby Code --
    > class CardStack<Array
    > def my_shuffle!
    > size.times do
    > push slice! rand(size)
    > end
    > self
    > end
    > end
    >
    > deck = CardStack.new
    >
    > deck = [1,2,3,4,5,6]
    >
    > deck.my_shuffle!


    Often sort_by is used for this:

    deck = deck.sort_by { rand }

    Kind regards

    robert
     
    Robert Klemme, Jun 12, 2006
    #5
  6. Guest

    Thanks for the reactions. It's clear now. This was my first "ruby
    program". I'm used to C++.

    --
    Bjorn
     
    , Jun 14, 2006
    #6
  7. Guest

    Thanks for the reactions. It's clear now. This was my first "ruby
    program". I'm used to C++.

    --
    Bjorn
     
    , Jun 14, 2006
    #7
    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. Replies:
    12
    Views:
    14,516
    Lee Ryman
    Apr 26, 2005
  2. James Edward Gray II

    Array.shuffle()?

    James Edward Gray II, Sep 23, 2004, in forum: Ruby
    Replies:
    1
    Views:
    113
    Robert Klemme
    Sep 23, 2004
  3. Pat Maddox

    sort_by{rand} doesn't shuffle array

    Pat Maddox, Jan 3, 2006, in forum: Ruby
    Replies:
    4
    Views:
    123
    Mauricio Fernandez
    Jan 3, 2006
  4. Jeff Moore

    Array.shuffle/Array.shuffle!

    Jeff Moore, Aug 24, 2008, in forum: Ruby
    Replies:
    6
    Views:
    156
    Alexei Broner
    Oct 9, 2008
  5. Tuxedo
    Replies:
    8
    Views:
    133
    Tuxedo
    May 7, 2007
Loading...

Share This Page