Skip to content

Instantly share code, notes, and snippets.

@kaibrabo
Created June 21, 2017 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaibrabo/70df1662aaa0262baec167738d72292b to your computer and use it in GitHub Desktop.
Save kaibrabo/70df1662aaa0262baec167738d72292b to your computer and use it in GitHub Desktop.
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