Created
June 22, 2017 02:31
-
-
Save kaibrabo/d98cb5bb919aca405ef3efe0f7d007f9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
=begin | |
Define a Title class which is initialized by a String. | |
It has one method --fix-- that returns a title-cased version of the String: | |
Title.new("a title of a book").fix | |
#=> A Title of a Book | |
We will need to use conditional logic --if and else statements -- to make this work. Read the test specification carefully so you understand the conditional logic to be implemented. | |
Some helpful methods to use are: | |
String#downcase | |
String#capitalize | |
Array#include? | |
You can also use each_with_index to detect when you're on the first word, which should always be capitalized. | |
=end | |
test: | |
describe Title do | |
describe "#fix" do | |
it "capitalizes the first letter of each word" do | |
expect( Title.new("the great gatsby").fix ).to eq("The Great Gatsby") | |
end | |
it "works for words with mixed cases" do | |
expect( Title.new("liTTle reD Riding hOOD").fix ).to eq("Little Red Riding Hood") | |
end | |
it "downcases articles" do | |
expect( Title.new("The lord of the rings").fix ).to eq("The Lord of the Rings") | |
expect( Title.new("The sword And The stone").fix ).to eq("The Sword and the Stone") | |
expect( Title.new("the portrait of a lady").fix ).to eq("The Portrait of a Lady") | |
end | |
it "works for strings with all uppercase characters" do | |
expect( Title.new("THE SWORD AND THE STONE").fix ).to eq("The Sword and the Stone") | |
end | |
end | |
end | |
code: <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
class Title | |
attr_accessor :string | |
WORDS = %w( the of a and ) | |
def initialize(string) | |
@string = string | |
end | |
def fix | |
words = string.split(' ') | |
words.map do |word| | |
word = word.downcase | |
if WORDS.include?(word) | |
word | |
else | |
word.capitalize | |
end | |
end.join(" ") | |
end | |
end | |
requirements: | |
=begin | |
Title#fix capitalizes the first letter of each word | |
RSpec::Expectations::ExpectationNotMetError | |
expected: "The Great Gatsby" | |
got: "the Great Gatsby" | |
(compared using ==) | |
exercise_spec.rb:7:in `block (3 levels) in <top (required)>' | |
Title#fix works for words with mixed cases | |
Title#fix downcases articles | |
RSpec::Expectations::ExpectationNotMetError | |
expected: "The Lord of the Rings" | |
got: "the Lord of the Rings" | |
(compared using ==) | |
exercise_spec.rb:15:in `block (3 levels) in <top (required)>' | |
Title#fix works for strings with all uppercase characters | |
RSpec::Expectations::ExpectationNotMetError | |
expected: "The Sword and the Stone" | |
got: "the Sword and the Stone" | |
(compared using ==) | |
exercise_spec.rb:21:in `block (3 levels) in <top (required)>' | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment