Ruby newbie here. I'm trying to create a method that 'corrects' titles of movies. The methods should capitalize properly, ignore capitalizing articles(a, an, the, of) unless at the beginning, and should correct properly even if the title given is all uppercase or jumbled. I get stuck when I want to specify a certain range of indexes in a string (1..100) to find articles and change them into lowercase. The first half of my code runs just fine, but the second half which modifies a range of indexes the .join is where i'm having trouble. If its possible to use .gsub's or 'if' statements after the ".join" that would be the best advice for my level of understanding. I hope I'm being clear. Any help/input is appreciated. Thank you
class Title
attr_accessor :title
def initialize(title)
@title = title
end
def fix
new_array = []
@title.split.each do |word|
new_array << "#{word}".capitalize
end
new_array.join(" ")
new_array(1..100).gsub("Of","of").gsub("The","the").gsub("And","and")
end
end
end
alternately:
class Title
attr_accessor :title
def initialize(title)
@title = title
end
def fix
new_array = []
@title.split.each do |word|
new_array << "#{word}".capitalize
end
new_array.join(" ")
if new_array(1..100) then
new_array.gsub("Of","of").gsub("The","the").gsub("And","and")
end
end
end
here are the specs
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
Aucun commentaire:
Enregistrer un commentaire