View card_deck.rb
This file contains 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
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) |
View poly-over-cond.rb
This file contains 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
# 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 |
View string_primitive_deck.rb
This file contains 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
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}" } |
View example-1.rb
This file contains 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 '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 |
View example-2.rb
This file contains 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
def lines_after_word(lines, target) | |
index = lines.find_index {|line| line =~ /#{target}/ } | |
index.nil? ? [] : "" | |
end |
View example-3.rb
This file contains 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 '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 |
View example-4.rb
This file contains 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
def lines_after_word(lines, target) | |
index = lines.find_index {|line| line =~ /#{target}/ } | |
index.nil? ? [] : lines[index..-1] | |
end |
View example-5.rb
This file contains 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 '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 |
View example-1.rb
This file contains 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
Rank = Struct.new(:rank, :value) do | |
def to_s; rank; end | |
end |
View example-2.rb
This file contains 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 'values' | |
class Rank < Value.new(:rank, :value) | |
def to_s | |
rank.to_s.capitalize | |
end | |
end |
OlderNewer