Created
January 15, 2016 03:15
-
-
Save framallo/8b33bd13cb545395a3c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'pp' | |
class Card < Struct.new(:suit, :rank, :color) | |
end | |
class Deck | |
attr_accessor :cards | |
def initialize | |
end | |
def suits | |
%w(clubs hearts spades diamonds) | |
end | |
def ranks | |
%w(ace 2 3 4 5 6 7 8 9 10 jack queen king) | |
end | |
def cards | |
@cards ||= build_cards | |
end | |
def shuffle | |
@cards.shuffle! | |
end | |
def deal | |
# shuffle.pop | |
shuffle.shift | |
end | |
private | |
def build_cards | |
cards = [] | |
suits.product(ranks) do |suit, rank| | |
cards.push Card.new(suit, rank) | |
end | |
cards | |
end | |
end |
This file contains hidden or 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
require_relative 'deck' | |
require 'rspec' | |
RSpec.describe Deck do | |
it 'has 52 cards' do | |
expect(Deck.new.cards.size).to eq 52 | |
end | |
describe '#deal' do | |
it 'reduces the amount of cards' do | |
deck = Deck.new | |
expect { deck.deal }.to change { deck.cards.size }.from(52).to(51) | |
end | |
end | |
end | |
# $ rspec deck_spec.rb | |
# F | |
# | |
# Failures: | |
# | |
# 1) Deck has 52 cards | |
# Failure/Error: expect(Deck.new.cards.size).to eq 52 | |
# | |
# NoMethodError: | |
# undefined method `size' for nil:NilClass | |
# # ./deck_spec.rb:6:in `block (2 levels) in <top (required)>' | |
# | |
# Finished in 0.00386 seconds (files took 0.35181 seconds to load) | |
# 1 example, 1 failure | |
# | |
# Failed examples: | |
# | |
# rspec ./deck_spec.rb:5 # Deck has 52 cards | |
# $ rspec deck_spec.rb | |
# . | |
# | |
# Finished in 0.00318 seconds (files took 0.36684 seconds to load) | |
# 1 example, 0 failures | |
# $ rspec deck_spec.rb | |
# F | |
# | |
# Failures: | |
# | |
# 1) Deck has 52 cards | |
# Failure/Error: expect(Deck.new.cards.size).to eq 52 | |
# | |
# expected: 52 | |
# got: 65 | |
# | |
# (compared using ==) | |
# # ./deck_spec.rb:6:in `block (2 levels) in <top (required)>' | |
# | |
# Finished in 0.02266 seconds (files took 0.42261 seconds to load) | |
# 1 example, 1 failure | |
# | |
# Failed examples: | |
# | |
# rspec ./deck_spec.rb:5 # Deck has 52 cards | |
# $ rspec deck_spec.rb | |
# .. | |
# | |
# Finished in 0.00473 seconds (files took 0.29317 seconds to load) | |
# 2 examples, 0 failures | |
# | |
# $ rspec deck_spec.rb | |
# .F | |
# | |
# Failures: | |
# | |
# 1) Deck#deal reduces the amount of cards | |
# Failure/Error: expect { deck.deal }.to change { deck.cards.size }.from(52).to(51) | |
# expected result to have changed from 52 to 51, but did not change | |
# # ./deck_spec.rb:12:in `block (3 levels) in <top (required)>' | |
# Finished in 0.02081 seconds (files took 0.26372 seconds to load) | |
# 2 examples, 1 failure | |
# | |
# Failed examples: | |
# | |
# rspec ./deck_spec.rb:10 # Deck#deal reduces the amount of cards | |
# | |
# $ rspec deck_spec.rb | |
# .. | |
# | |
# Finished in 0.00442 seconds (files took 0.27671 seconds to load) | |
# 2 examples, 0 failures |
This file contains hidden or 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
require_relative 'deck' | |
deck = Deck.new | |
pp deck.build_deck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment