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) |
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 |
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}" } |
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 |
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 |
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 |
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 |
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 |
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 |
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