Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / card_deck.rb
Created March 28, 2014 18:08
Functional/OO Deck of Cards in Ruby - Start of a series of articles for rubylove.io
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)
@thatrubylove
thatrubylove / poly-over-cond.rb
Created April 1, 2014 18:18
Polymorphism over conditionals
# Conditional reach-around
User = Struct.new(:name, :role) do; end
def greet(user)
if user.name && user.role
"Hello, I am an #{user.role} user. My name is #{user.name}."
elsif user.name
"Hello, my name is #{user.name}."
else
@thatrubylove
thatrubylove / string_primitive_deck.rb
Created April 2, 2014 08:48
one liner magic - but it is primitive obsessed with a string
DECK = %w[Ace 2 3 4 5 6 7 8 9 10 Jack Queen King].product(%w[Clubs Diamonds Hearts Spades]).map {|rank, suit| "#{rank} of #{suit}" }
@thatrubylove
thatrubylove / example-1.rb
Last active August 29, 2015 13:59
tdding-patshaughnessys-ask-dont-tell
require 'minitest/autorun'
describe "#lines_after_word(lines, target)" do
let(:file) { File.expand_path("../innisfree.txt", __FILE__) }
let(:lines) { File.readlines(file) }
it "must return an empty array if no lines include the target word" do
lines_after_word(lines, "hola").must_equal []
end
end
@thatrubylove
thatrubylove / example-2.rb
Created April 16, 2014 02:38
tdding-patshaughnessys-ask-dont-tell - example-2.rb
def lines_after_word(lines, target)
index = lines.find_index {|line| line =~ /#{target}/ }
index.nil? ? [] : ""
end
@thatrubylove
thatrubylove / example-3.rb
Created April 16, 2014 02:41
tdding-patshaughnessys-ask-dont-tell-example-3.rb
require 'minitest/autorun'
describe "#lines_after_word(lines, target)" do
let(:lines) { File.readlines(File.expand_path("../innisfree.txt", __FILE__)) }
it "must return all the lines if the first line includes the target word" do
lines_after_word(lines, "Innisfree").count.must_equal lines.count
end
end
@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