Codecademy Ruby course: Control Flow know-how - ruby

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

Related

Error in .each execution (Ruby)

I'm making an auditor with ruby which started off fine this morning (using single word, user inputted content to omit) but now that I've tried to implement a wordlist, it puts the string to search through as many times as there are words in the wordlist, only censoring it once or twice. My code is as follows.
#by Nightc||ed, ©2015
puts "Enter string: "
text = gets.chomp
redact = File.read("wordlist.txt").split(" ")
words = text.split(" ")
redact.each do |beep|
words.each do |word|
if word != beep
print word + " "
else
print "[snip] "
end
end
end
sleep
I kind of understand why it doesn't work but I'm not sure how to fix it.
Any help would be appreciated.
There's an easier way than iterating through each array. The Array#include method can be easily used to see if the word is contained in your redacted list.
Here's some code that should behave how you wanted the original code to behave:
puts "Enter string: "
text = gets.chomp
redact = File.read("wordlist.txt").split(" ")
words = text.split(" ")
words.each do |word|
if redact.include? word
print "[snip] "
else
print word + " "
end
end
Scrubbing text gets very tricky. One thing you want to watch out for is word boundaries. Splitting on spaces will let a lot of beep words get through because of puctuation. Compare the first two results of the sample code below.
Next, assembling the split text back into its intended form with punction, spacing, etc., gets to be quite challenging. You may want to consider using regex for something presuambly as small as user comments. See the third result.
If you're doing this as a learning exercise, great, but if the application is sensitive where you're likely to take heat over failures to bleep words, you may want to look for an existing well-tested library.
#!/usr/bin/env ruby
# Bleeper
scifi_curses = ['friggin', 'gorram', 'fracking', 'dork']
text = "Why splitting spaces won't catch all the friggin bleeps ya gorram, fracking dork."
words = text.split(" ")
words.each do |this_word|
puts "bleep #{this_word}" if scifi_curses.include?(this_word)
end
puts
better_words = text.split(/\b/)
better_words.each do |this_word|
puts "bleep #{this_word}" if scifi_curses.include?(this_word)
end
puts
bleeped_text = text # keep copy of original if needed
scifi_curses.each do |this_curse|
bleeped_text.gsub!(this_curse, '[bleep]')
end
puts bleeped_text
You should get these results:
bleep friggin
bleep fracking
bleep friggin
bleep gorram
bleep fracking
bleep dork
Why splitting spaces won't catch all the [bleep] bleeps ya [bleep], [bleep] [bleep].

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+" "
}

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:

Resources