Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Shuffle Array.

Reply
Thread Tools

Shuffle Array.

 
 
bjorn2k@hotmail.com
Guest
Posts: n/a
 
      06-12-2006
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

 
Reply With Quote
 
 
 
 
ekofoed
Guest
Posts: n/a
 
      06-12-2006
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


 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      06-12-2006
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
 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      06-12-2006
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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-12-2006
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
 
Reply With Quote
 
bjorn2k@hotmail.com
Guest
Posts: n/a
 
      06-14-2006
Thanks for the reactions. It's clear now. This was my first "ruby
program". I'm used to C++.

--
Bjorn

 
Reply With Quote
 
bjorn2k@hotmail.com
Guest
Posts: n/a
 
      06-14-2006
Thanks for the reactions. It's clear now. This was my first "ruby
program". I'm used to C++.

--
Bjorn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array.shuffle/Array.shuffle! Jeff Moore Ruby 6 10-09-2008 07:14 PM
Ipod shuffle help please Paul Burdett Computer Support 2 08-14-2005 10:01 PM
iPod Shuffle + Windows XP Limited User Account Goonerak Computer Support 0 06-12-2005 01:16 PM
Shuffle an array of integers. pieter_hordijk@hotmail.com Java 12 04-26-2005 05:31 AM
shuffle stickems Computer Support 1 07-27-2003 07:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57