Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / example-3.rb
Created April 16, 2014 04:50
applying-design-patterns-to-ruby-example-3
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit}"; end
end
@thatrubylove
thatrubylove / example-4.rb
Last active August 29, 2015 13:59
applying-design-patterns-to-ruby-example-4
require 'values'
require 'rank'
class Card < Value.new(:rank, :suit)
def suit
(@suit || 'naked').to_sym
end
def to_s
"#{rank.to_s} of #{suit.to_s.capitalize}"
@thatrubylove
thatrubylove / example-1.txt
Created April 16, 2014 04:59
a-faux-o-deck-of-cards-example-1
A _Deck_ of _Card_s is used to play many different types of games.
_Card_s have a _Rank_ and a _Suit_ and depending on the rules of the game,
these are used to score. Also, many games require a _shoe size_, which
specifies how many standard _Deck_s of _Card_s to include in the generated
_Deck_.
@thatrubylove
thatrubylove / example-2.rb
Created April 16, 2014 05:03
a-faux-o-deck-of-cards-example-2
class Deck
attr_reader :cards
def initialize(show_size=1)
@cards = build_deck*shoe_size
end
end
@thatrubylove
thatrubylove / example-3.rb
Created April 16, 2014 05:04
a-faux-o-deck-of-cards-example-3
Rank = Struct.new(:rank, :value) do
def to_s; rank; end
end
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit}"; end
end
@thatrubylove
thatrubylove / example-4.rb
Created April 16, 2014 05:04
a-faux-o-deck-of-cards-example-4
Deck.new.cards[24].to_s #=> "seven of hearts"
@thatrubylove
thatrubylove / example-5.rb
Created April 16, 2014 05:05
a-faux-o-deck-of-cards-example-5
class Deck
SUITS = %w(hearts clubs spades diamonds)
RANKS = %w(ace two three four five six seven
eight nine ten jack queen king)
...
end
@thatrubylove
thatrubylove / example-6.rb
Created April 16, 2014 05:06
a-faux-o-deck-of-cards-example-6
class Deck
...
protected
def all_ranks
RANKS.map.with_index {|rank, value| Rank.new(rank, value) }
end
end
@thatrubylove
thatrubylove / example-7.rb
Created April 16, 2014 05:07
a-faux-o-deck-of-cards-example-7
class Deck
...
protected
def build_deck
all_ranks.flat_map {|rank| SUITS.flat_map {|suit| Card.new(rank,suit) } }
end
...
@thatrubylove
thatrubylove / example-8.rb
Created April 16, 2014 05:07
a-faux-o-deck-of-cards-example-8
Rank = Struct.new(:rank, :value) do
def to_s; rank; end
end
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit}"; end
end
class Deck
SUITS = %w(hearts clubs spades diamonds)