The Deck of Cards: A great intro to OOP

When I first started getting into object-oriented programming, I thought about things around the house I could model to help make more sense of what exactly OOP is. I personally feel that creating a deck of cards in ruby is what helped me get over the initial hump of wrapping my head around it.

Below, I wrote a bit of code that could be used as a base template for a deck of cards with aces high.

class Card

 attr_reader :number, :suit, :value

 def initialize(number, suit, value)
 @number = number
 @suit = suit
 @value = value
 end

end

class Deck
 
 attr_reader :deck
 
 def initialize
 @deck = [
 Card.new("2", "Hearts", 1),
 Card.new("3", "Hearts", 2),
 Card.new("4", "Hearts", 3),
 Card.new("5", "Hearts", 4),
 Card.new("6", "Hearts", 5),
 Card.new("7", "Hearts", 6),
 Card.new("8", "Hearts", 7),
 Card.new("9", "Hearts", 8),
 Card.new("10", "Hearts", 9),
 Card.new("Jack", "Hearts", 10),
 Card.new("Queen", "Hearts", 11),
 Card.new("King", "Hearts", 12),
 Card.new("Ace", "Hearts", 13),
 Card.new("2", "Spades", 1),
 Card.new("3", "Spades", 2),
 Card.new("4", "Spades", 3),
 Card.new("5", "Spades", 4),
 Card.new("6", "Spades", 5),
 Card.new("7", "Spades", 6),
 Card.new("8", "Spades", 7),
 Card.new("9", "Spades", 8),
 Card.new("10", "Spades", 9),
 Card.new("Jack", "Spades", 10),
 Card.new("Queen", "Spades", 11),
 Card.new("King", "Spades", 12),
 Card.new("Ace", "Spades", 13),
 Card.new("2", "Diamonds", 1),
 Card.new("3", "Diamonds", 2),
 Card.new("4", "Diamonds", 3),
 Card.new("5", "Diamonds", 4),
 Card.new("6", "Diamonds", 5),
 Card.new("7", "Diamonds", 6),
 Card.new("8", "Diamonds", 7),
 Card.new("9", "Diamonds", 8),
 Card.new("10", "Diamonds", 9),
 Card.new("Jack", "Diamonds", 10),
 Card.new("Queen", "Diamonds", 11),
 Card.new("King", "Diamonds", 12),
 Card.new("Ace", "Diamonds", 13),
 Card.new("2", "Clubs", 1),
 Card.new("3", "Clubs", 2),
 Card.new("4", "Clubs", 3),
 Card.new("5", "Clubs", 4),
 Card.new("6", "Clubs", 5),
 Card.new("7", "Clubs", 6),
 Card.new("8", "Clubs", 7),
 Card.new("9", "Clubs", 8),
 Card.new("10", "Clubs", 9),
 Card.new("Jack", "Clubs", 10),
 Card.new("Queen", "Clubs", 11),
 Card.new("King", "Clubs", 12),
 Card.new("Ace", "Clubs", 13)
 ]
 end

 def mix
 @deck.shuffle!
 end

 def deal
 @deck.shift
 end

 def cards_left
 @deck.length
 end

end

# using the classes:

#create the deck and assign it a variable
new_full_deck = Deck.new

#shuffle the cards
new_full_deck.mix

#deal a card
new_full_deck.deal
=> <CARD OBJECT>

A deck of cards is an object that has many objects (cards) that are very familiar to our brains, so creating a deck of other card object really helped me understand OOP.

The Deck of Cards: A great intro to OOP