I am a Ruby Newbie and I'm having some trouble with my Title class below. I have the first two logical requirements met, but the third one I am having some trouble with. I have researched on StackOverFlow and Google and I have seen how you can use the if structure in the map method and I'm not sure why this isn't working for me. Can anyone offer any assistance/guidance?
- It is successfully taking an all lower case string like "the united states" and making each 1st letter in each word capitalized(The United States)in the string.
It is successful in taking a camel case string like "ThE UnIted STatEs" and making it "The United States".
I am trying to add the additional logic where if the string is "the", "The", "of", "Of" it does not capitalize it. I received the error of: wrong number of arguments (2 for 1). Is there another way I can implement this logic?
The attempted map logic to the Title Class that did not work | Error received: wrong number of arguments (2 for 1)
class Title
attr_accessor :string
def initialize(string)
@string = string
end
def fix
string2 = string.split(" ").map{ |string| string.capitalize }.join(" ")
string2.split(" ").map{ |string| (string.include?("of","Of","the","The") ? string.downcase : string.capitalize) }.join(" ")
end
end
Title Class that works fine for #1 & #2
class Title
attr_accessor :string
def initialize(string)
@string = string
end
def fix
string2 = string.split(" ").map{ |string| string.capitalize }.join(" ")
end
end
Aucun commentaire:
Enregistrer un commentaire