Printing a redacted word from an if loop in RUBY - 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+" "
}

Related

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(' ')

Ruby redaction not printing

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:

Chris Pine Learn to Program Ruby Ch 8: alphabetize your array

Getting stuck on chapter 8:
Type as many words as we want
One word per line, continuing until we just press Enter on an empty line
Repeats the words back to us in alphabetical order.
Use 'sort'
So, here's what I got to, but I'm having funny issues with not getting the first word to push into the array [among other things]
# alphabetting
puts 'Tell us some of your favorite things!'
# create an array
words = []
while gets.chomp != ''
words.push gets.chomp
words.sort
puts words
end
Did this and it works now... Do I have to have "thing" in there though? Seems naughty to assign within a 'while' loop.
puts 'Tell us some of your favorite things!'
words = []
puts words
while (thing = gets.chomp) != ''
words.push thing
end
puts words.sort
Your first gets call is not referred to by anything, and is thrown out. It is not just the first word, but every other word that is going to be thrown out. The output routine should also be outside of the loop. A fix is:
words = []
while word = gets.chomp and not word.empty?
words.push(word)
end
puts words.sort
Try this:
puts 'Tell us some of your favorite things!'
words = []
while line = STDIN.gets
line = line.chomp
break if line.empty?
words << line.chomp
end
words = words.sort
words.each {|word| puts word }

Resources