Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coxandrew/607884 to your computer and use it in GitHub Desktop.
Save coxandrew/607884 to your computer and use it in GitHub Desktop.
# Original module with "magic numbers" for 4 suits and 13 card values
# See: http://refactoring.com/catalog/replaceMagicNumberWithSymbolicConstant.html
module PlayingCards
SUITS = %w{ spades clubs hearts diamonds }
class Deck
attr_accessor :cards
def initialize
@cards = []
4.times do |s|
13.times do |r|
@cards << Card.new(r, SUITS[s])
end
end
end
end
class Card
def initialize(rank, suit)
@rank, @suit = rank, suit
end
end
end
module PlayingCards
SUITS = %w{ spades clubs hearts diamonds }
RANKS = %w{ 2 3 4 5 6 7 8 9 10 J Q K A }
class Deck
attr_accessor :cards
def initialize
@cards = []
SUITS.each do |suit|
RANKS.each do |rank|
@cards << Card.new(rank, suit)
end
end
end
end
class Card
def initialize(rank, suit)
@rank, @suit = rank, suit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment