Tim Hoolihan
6/12/2006 1:39:00 PM
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
>