Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / example-4.rb
Created April 16, 2014 02:42
tdding-patshaughnessys-ask-dont-tell-example-4.rb
def lines_after_word(lines, target)
index = lines.find_index {|line| line =~ /#{target}/ }
index.nil? ? [] : lines[index..-1]
end
@thatrubylove
thatrubylove / example-5.rb
Created April 16, 2014 02:45
tdding-patshaughnessys-ask-dont-tell-example-5.rb
require 'minitest/autorun'
describe "#lines_after_word(lines, target)" do
let(:lines) { File.readlines(File.expand_path("../innisfree.txt", __FILE__)) }
it "must return the last 7 lines if the 8th line matches the target word" do
lines_after_word(lines, "glimmer").count.must_equal 7
end
end
@thatrubylove
thatrubylove / example-1.rb
Created April 16, 2014 04:24
2014-03-29-applying-design-patterns-to-ruby-example-1
Rank = Struct.new(:rank, :value) do
def to_s; rank; end
end
@thatrubylove
thatrubylove / example-2.rb
Created April 16, 2014 04:48
applying-design-patterns-to-ruby-example-2
require 'values'
class Rank < Value.new(:rank, :value)
def to_s
rank.to_s.capitalize
end
end
@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"