FAQ: Thith Meanth War! - Returning the Final String—Er, "Thtring"

This community-built FAQ covers the “Returning the Final String—Er, “Thtring”” exercise from the lesson “Thith Meanth War!”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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

You can create a copy of the original string to preserve its initial value, then manipulate the original, and print both in the final puts statement.

puts "#{original} => #{mutated}"
1 Like
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…

1 Like

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.

print “string please!”
user_input = gets.chomp
user_input.downcase!

if user_input.include? “s”
user_input.gsub!(/s/, “th”)
else
puts “no s’s”
end
user_input = “woop”
print “poop scoop, #{user_input}!”

When working with this code I am getting a check that says I have the right answer but no Daffy duckification is happening.
WHy?

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.

why did you create another string in the end for user_input? isn’t it supposed to change your original string?

1 Like

Hi, im stuck at 7/8 of the exercise on Daffy Duckification
here is my code doesn’t WORK. need help pls

print "I like the sea "

user_input = gets.chomp

user_input.downcase!

if user_input.include? “s”

user_input.gsub!(/s/, “th”)

else

puts “I still like the sea”

end

puts “I like the sea: #{user_input}”

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

Thanks!

1 Like

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.

Suggest do not use the bang method.

user_input = gets.chomp
new_string = user_input.downcase

What happens, then?

1 Like

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.

Related thread:

Hi, I hope someone can help.

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

Thank you for reading.

After getting the user’s input, your code only prints anything when the input doesn’t contain any s’s.