Shuffle Array.

B

bjorn2k

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.
 
E

ekofoed

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

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.
 
M

Martin DeMello

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
 
M

Martin DeMello

ekofoed said:
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
 
R

Robert Klemme

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
 
B

bjorn2k

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

bjorn2k

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top