Thith Meanth War! 8/8


#1




Hi everyone, I'm doing the code challenge for this chapter and came up with my code that's working for all three questions. I'm thinking more from a linguistic point of view. For Q2, I'm thinking more of "ce" that sounds like s. As for remaining the capitalization of the original user input, I simply put possible combinations in the conditions. It works well. But I want to know if I don't list out all the possibilities, what method can be used to preserve the original capitalization?


puts "Please type in words here:"
user_input = gets.chomp

if  user_input.empty?
    puts "There's no words here."
elsif
    user_input.include?"s" || "ce" || "CE" || "Ce" || "S"
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/ce/, "the")  
    user_input.gsub!(/CE/, "THE")
    user_input.gsub!(/Ce/, "THe") 
    user_input.gsub!(/S/, "TH") 
    puts "Daffified sentence: #{user_input}" 
else
    print "Please enter words with 's' or 'ce'"
end


#2

From the lesson:
Call the .downcase! method on user_input. Make sure to include the ! so that the user's string is modified in-place; otherwise, Ruby will create a copy of user_input and modify that instead.

basically, this is saying that if you don't use !, then it will modify a separate string automatically without you asking it too. I think you're wanting to not modify the actual string right?
I don't know exactly how to answer your question, but i thought this might be something you want to hear.


#3

Hi kyleaw, thanks for the tip. I actually thought of the downcase!, but once user_input changes to downcase, it won't be able to preserve the original capitalization in the output. So I didn't use it. I wonder if you use downcase!, how do you find those "s"s and change them back to their original capitalized letters?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.