FAQs on the exercise Returning the Final String—Er, “Thtring”
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
how to preserve the original user input and return the original and modified version with print command because after the gsub method the user_input always changes
print "Please enter a string: "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
puts "The 's' has been remplaced and now your string is equal to : #{user_input.gsub!(/s/, "th")}"
else
puts "There are no 's's in your string."
end
hello !
this code above gave me the right output but the exercise kept on giving me a “wrong answer”, i don’t understand…
Is any of the verbosity expected by the lesson checker? When we embellish our responses the SCT can no longer recognize anything so will fail the submission. Remember it is a program that is checking our code, not a human reader.
When using the bang method (!) it is imperative that there be at least one uppercase letter in the input else nil is the outcome. nil has no attributes or methods.
To get around this problem just remove the bang method and assign it to a variable.
user_input = input.downcase
So either solution will work. Use one or more uppercase with the bang, or any case without the bang.
Hi!
Can someone help me to understand why the downcase! method is afecting both variables: user_input and new_string at the same time? Note: try it using a upcase character when giving the input.
print "Input text: "
user_input = gets.chomp
new_string = user_input
new_string.downcase!
if new_string.include? "s"
new_string.gsub!(/s/, "th")
puts "Original text: #{user_input}."
puts "Output text: #{new_string}."
else
puts "Nothing to do here"
end
The only explanation one can come up with is that new_string is the same string object as user_input. That is, both variables point to the same object.
As mentioned in the “Overview & Sneak Peek” lesson:
Object-oriented, meaning it allows users to manipulate data structures called objects in order to build and execute programs. We’ll learn more about objects later, but for now, all you need to know is everything in Ruby is an object.
new_string = user_input results in both variables referring to the same string object. This is unlike many other languages where we don’t see similar behavior regarding strings.
The solution by mtf will work because new_string = user_input.downcase will end up creating a new string object.
Can anyone please explain why when I use a word with an s I don’t get the word Daffy Duckified?
print "Pleathe enter a thtring: "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "There are no 's's in your string."
print "See here, (#{user_input})!"
end