Ruby redaction not printing - ruby

I am running through the Codecademy lessons to get up to speed with the basics of Ruby but I hit a snag with their redacted! exercise. I posted what I think should do the trick but it refuses to print.
The exercise instructions are as such :
Let's start simple: write an .each loop that goes through words and just prints out each word it finds.
Please help if you can. Thank you! Here is my code :
puts "What is your text, bra?"
text = gets.chomp
text.downcase!
puts "What is being hidden?"
redact= gets.chomp
redact.downcase!
words = text.split(" ")
eachredact = redact.split(" ")
#loop through the words in the string
words.each do |x|
print "#{x}" + " "
end

I ran it as below :
puts "What is your text, bra?"
text = gets.chomp
text.downcase!
puts "What is being hidden?"
redact= gets.chomp
redact.downcase!
words = text.split(" ")
eachredact = redact.split(" ")
#loop through the words in the string
words.each do |x|
print "#{x}" + " "
end
Output:
What is your text, bra?
i anm
What is being hidden?
Good
i anm
see here:

Related

What can't I redact certain words by using this ruby code?

I"m learning ruby on codecademy and I'm struggling with some ruby loop/iteration codes. This is not a duplication of How to re-prompt and re-use a user's input, because I didn't want to ask how to reprompt the user's input, but how to print out the redacted output. Below is the code example and the codes meant to change/redact certain words in the user's input:
puts "Please enter the text"
text = gets.chomp
text.downcase!
words = text.split(" ")
#ask for user input and turn the input into an array
puts "Please enter the redact word"
redact = gets.chomp
redact.downcase!
redacts = redact.split(" ")
#ask the user what words they want to redact in their input
words.each do |word|
if word == redacts.[i]
word = "REDACTED "
else
word = word
end
print word + " "
end
#print out the redacted input (including irredacted and redacted words)
But this code doesn't seem to work...I'm not sure why...Can you help me on this? Thank you!
Try the following version:
I am looping through all the redacted words and removing them from the input words array
puts "Please enter the text"
text = gets.chomp
text.downcase!
words = text.split(" ")
#ask for user input and turn the input into an array
puts "Please enter the redact word"
redact = gets.chomp
redact.downcase!
redacts = redact.split(" ")
#ask the user what words they want to redact in their input
redacts.each do |redact|
words.delete(redact)
end
#print out the redacted input (including irredacted and redacted words)
print(words.join(" "))

Codecademy Ruby course: Control Flow know-how

When coding in Ruby, I came up with an error about needing to state all words the user inputed. I tried to change my code to get it to output that, but the problem remained. Here is my code and the Ruby instructions.
Instructions
Add an if/else statement inside your .each.
if the current word equals the word to be redacted, then print "REDACTED " with that extra space.
Otherwise (else), print word + " ".
The extra space in both cases prevents the words from running together.
puts text = gets.chomp
puts redact = gets.chomp
words = text.split(" ")
words = ['hi', 'hello', 'what', 'why']
words.each do |word|
if gets = words
print "Redact "
else
print word + "Incorrect"
end
end
The problem it says I have with my code is... Oops, try again. Make sure to print each word from the user's text to the console unless that word is the word to be redacted; if it is, print REDACTED (all caps!).
I would appreciate all help, please and thank you.
Ben sorry these guys aren't being too helpful. :D It's been awhile but I hope this helps. In the first section titled "What you'll be building" Codecademy gives you the exact example (the answer) to the final problem in the section. This is always true and may help in the future. What you're looking for:
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(" ")
words.each do |word|
if word != redact
print word + " "
else
print "REDACTED "
end
end
Have pass several years I know, but I wanted pass and let my solution for this excercise.
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(',') # "," is necessary to identify each of the words
words.each do |x|
if x == redact # if words repeat, print REDACTED
print "REDACTED"
else # else, only write de word and space
print x + " "
end
end

Ruby downcase method not working as expected

I am writing a small bit of code in Ruby that is supposed to redact a word that is specified by the user, regardless if the word that is being passed is all uppercase, lowercase or a combination of the two. The way that I tried to get around this was by just using the downcase! method on the strings being passed by the user. However, it would seem that it does not work correctly. For example, if the first string that is passed and stored in the variable "text" is in all uppercase and the second string that is passed and stored in the variable "redact" is all downcase, the program will fail to redact the word and will just print out everything in downcase.
Here is the code below:
puts "Enter what you want to search through"
text = gets.chomp.downcase!
puts "Enter word to be redacted"
redact = gets.chomp.downcase!
words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end
The problem is that you use downcase! which will return nil if no change is made. The string itself is modified but the returned value is nil, which you save after in your text variable.
See the documentation about downcase and downcase! to understand the difference.
puts "Enter what you want to search through"
text = gets.chomp.downcase
puts "Enter word to be redacted"
redact = gets.chomp.downcase
words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end
try it without the exclamation marks.

How to keep a loop within a loop from repeating an output in Ruby

Hey I'm just starting to learn Ruby over at codeacademy and I've come across an exercise that had an optional challenge that I can't get. The program takes a user defined string and "redacts" the words that the user wants "redacted". As you can see it turns the strings into arrays and then cycles through each value in the arrays (aka the individual words) and attempts to cross check the values of the original string with those of the words the user wants "redacted". The original problem was to get it to redact one variable, the challenge was to get multiple words redacted. My problem is that when it goes through the checking loops it keeps returning values wrong. I understand what's wrong, sort of, the looping has to cross check values more than once and when it fails to '==' it keeps on printing out the word, but is there a way to fix this? Or is there a better angle to approach this?
puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = redact.split(" ")
words = text.split(" ")
words.each do |single_word|
bye_words.each do |word_in_question|
if word_in_question == single_word
print "REDACTED "
else
print single_word + " "
end
end
end
Maybe just use include? instead of another loop:
puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = redact.split(" ")
words = text.split(" ")
words.each do |single_word|
if bye_words.include? single_word
print "REDACTED "
else
print single_word + " "
end
end
Or even gsub but maybe that's cheating...?
How about something like this? You dont have to loop through two different sets:
puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = redact.split(" ")
words = text.split(" ")
ouput_words = []
words.each do |single_word|
if bye_words.include?(single_word)
ouput_words << "REDACTED"
else
ouput_words << single_word
end
end
print ouput_words.join(" ") + "\n"
The fastest way to get (almost) what you need is using -:
puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = redact.split(" ")
words = text.split(" ")
puts (words - bye_words).join(' ')
The problem is that the redacted words will simply be removed from the text ('REDACTED' will not be added).
For that requirement to be filled, you need to find if each word is included? in the bye_words. What can do to improve on the others' suggestions is to turn the list of bye_words to a Set.
require 'set'
puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = Set.new(redact.split(" "))
words = text.split(" ")
puts words.map { |word| bye_words.include?(word) ? 'REDACTED':word }.join(' ')

Printing a redacted word from an if loop in RUBY

I am going through the exercises in codecademy and am stuck at a place where my code isn't doing what I need it to do. All I need it to do is print the words but if the word is redacted, I need it to print "REDACTED". From what I can see, that is what my code is doing but I must have a symbol in the wrong place or something is missing. So, if anyone can see where I went wrong, I sure would appreciate a nudge in the right direction! Thanks you kindly! Here's my code :
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact print "REDACTED"+" " else print x+" "}
I found the answer. Thank you all anyway!
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact then print "REDACTED"+" " else print "#{x}"+" " end}
words is an array, and redact is a string. So you can't compare them using ==. What you're trying to do is see if redact is present anywhere in words. You can do this using include?:
if words.include?(redact) ...
An even better way to implement this would be to use a regex on the original input string:
print text.gsub(/\b#{redact}\b/, 'REDACTED')
Use $end instead of end for
words.each
{
|x|
if x == redact
print "REDACTED"+" "
else
print x+" "
}

Resources