FAQ: Redacted! - Getting the User's Input

This community-built FAQ covers the “Getting the User’s Input” exercise from the lesson “Redacted!”.

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

Learn Ruby

FAQs on the exercise Getting the User’s Input

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!

Now I’m confused. I thought print was used for prompts and puts was for only outputting text. Why is puts used here?

puts and print are pretty much the same thing. puts just creates a new (blank) line after whatever you wanted to display.

The print command just takes whatever you give it and prints it to the screen. puts (for "put string") is slightly different: it adds a new (blank) line after the thing you want it to print. You use them like this:

Either can be used for prompts since it’s just a text phrase. The consideration of which to use is where we wish to position the draw pencil for the next printed character to appear.

print "Height? "
height = gets.chomp
puts "You entered height of, #{height} meters."

On the console will appear, with given input,

Height? 1.9
You entered height of, 1.9 meters.

Consider another scenario…

print "Enter some text: "
text = "#{gets.chomp} "
print "Enter more text: "
text += gets.chomp
puts "Words to redact (separate by space)"
redact = gets.chomp
redact = redact.split(' ')

which on the console will read, with given inputs, (and as a result of the redaction code, not shown)

Enter some text: Hello
Enter more text: World
Words to redact (separate by space)
Hello World

Note the placement of the inputs when print is used, compared to when puts is used.

print "Enter some text: "
text = "#{gets.chomp} "
print "Enter more text: "
text += gets.chomp
puts text
z = text.dup
puts "Words to redact (separate by space)"
gets.chomp.split(' ').each {|x| z.gsub! /\b#{x}\b/i, "REDACTED"}
puts z
Enter some text: foo faz
Enter more text: bar baz
foo faz bar baz
Words to redact (separate by space)
faz baz
1 Like

I have tried several times but the code will not let me go through to the next section despite even using the prompted solution out of sheer frustration at the endless attempts to pass it. I’m begging here for my sanity, help me.

I got stuck too. The answer is to have a blank line (press enter twice) after you declare the text variable.

Literally adding an extra carriage return will let you proceed. As you rightly point out, it’s crazy because Ruby isn’t supposed to care about whitespace, and the very example on the previous page was laid out without blank lines.