I think a Deck of cards can be modeled by an Array. So let’s say I do this:
class Deck < Array
def initialize
super (1..52).to_a
end
end
The problem I am having is this. I really like to use the inherited “generator methods”. e.g.:
d = Deck.new
d.shuffle!
d.class --> this gives Deck... awesome
However, this loses the “Deck” class:
e = d.shuffle
d.class --> this gives Array
How can I make all the inherited Array generator methods cast the results back to Deck? (that is without typing out each of the methods in the Deck class… that seems to defeat the purpose of inheritance).
This also seems like a less than optimized method… offloading the task to the caller:
You’ll have to create a shuffle method in Deck which returns a Deck. Are you sure a Deck ‘is a’ Array? Maybe its better to think of a Deck as having an(has a) array of Cards. If you choose the latter then you can create a Card class which will contain a trump and value.