Forked from kaibrabo/gist:70df1662aaa0262baec167738d72292b
Last active
June 21, 2017 22:04
-
-
Save flagoworld/fd6251f173d73fd4a4b3bd00bf379315 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
Hey guys! | |
I am a little stuck on Title Case checkpoint on Ruby Arrays. | |
“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.” | |
``` | |
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 | |
``` | |
If anyone has any insight, please help :slightly_smiling_face: here’s my code: | |
``` | |
class Title | |
attr_accessor :string | |
ignore = %w( the of a and ) | |
def initialize(string) | |
@string = string | |
end | |
def fix | |
new_words = string.split(' ') | |
new_words.map do |word| | |
word = word.downcase | |
# if ignore.include?(word) | |
# word | |
# else | |
# word.capitalize | |
# end | |
end.join(" ") | |
end | |
end | |
Title.new("The sword And The stone").fix | |
#=> "the sword and the stone" | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment