[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Shuffle Array.

bjorn2k

6/12/2006 1:10:00 PM

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

7 Answers

ekofoed

6/12/2006 1:20:00 PM

0

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

bjorn2k@hotmail.com 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

Tim Hoolihan

6/12/2006 1:39:00 PM

0

After this line:
deck = [1,2,3,4,5,6]
If you put in:
puts deck.class
You'll see that your deck is now an array, not a Card Stack. You either
need to override some operators, or just have your CardStack use an
array, like:

class CardStack
attr_reader :cards
attr_writer :cards
def initialize
@cards=[]
end
def my_shuffle!
@cards.size.times do
@cards.push(@cards.slice!(rand(@cards.size)))
end
self
end
def to_s
@cards.join(",")
end
end

deck=CardStack.new
deck.cards=(1..6).to_a
deck.my_shuffle!
puts deck.to_s
-Tim

bjorn2k@hotmail.com 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
>

Martin DeMello

6/12/2006 1:51:00 PM

0

bjorn2k@hotmail.com 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

6/12/2006 2:00:00 PM

0

ekofoed <ekofoed@gmail.com> 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

Robert Klemme

6/12/2006 7:59:00 PM

0

bjorn2k@hotmail.com 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

bjorn2k

6/14/2006 2:19:00 PM

0

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

--
Bjorn

bjorn2k

6/14/2006 2:19:00 PM

0

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

--
Bjorn