-
-
Save coxandrew/607884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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