Skip to content

Instantly share code, notes, and snippets.

Jim OKelly thatrubylove

Block or report user

Report or block thatrubylove

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@thatrubylove
thatrubylove / card_deck.rb
Created Mar 28, 2014
Functional/OO Deck of Cards in Ruby - Start of a series of articles for rubylove.io
View card_deck.rb
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 Apr 1, 2014
Polymorphism over conditionals
View poly-over-cond.rb
# 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 Apr 2, 2014
one liner magic - but it is primitive obsessed with a string
View string_primitive_deck.rb
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-5.rb
Created Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell-example-5.rb
View 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-3.rb
Created Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell-example-3.rb
View 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-2.rb
Created Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell - example-2.rb
View example-2.rb
def lines_after_word(lines, target)
index = lines.find_index {|line| line =~ /#{target}/ }
index.nil? ? [] : ""
end
@thatrubylove
thatrubylove / example-4.rb
Created Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell-example-4.rb
View 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-1.rb
Last active Aug 29, 2015
tdding-patshaughnessys-ask-dont-tell
View example-1.rb
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-9.rb
Created Apr 16, 2014
a-faux-o-deck-of-cards-example-9
View example-9.rb
blackjack_deck = Deck.new(8)
poker_deck = Deck.new
@thatrubylove
thatrubylove / example-4.rb
Created Apr 16, 2014
a-faux-o-deck-of-cards-example-4
View example-4.rb
Deck.new.cards[24].to_s #=> "seven of hearts"
You can’t perform that action at this time.