Redacted!


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/6?curriculum_id=5059f8619189a5000201fbcb


Hello everyone. I have questions related ot the end of this excersize, which invites you to take your cod e to the next step. It asks you:
What could you do to make sure your redactor redacts a word regardless of whether it's upper case or lower case?
How could you make your program take multiple, separate words to REDACT?
How might you make a new redacted string and save it as a variable, rather than just printing it to the console?

Franky, I am not sure exactly how to do these. Any insight or help would be much appreciated. Thank you.


https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/6?curriculum_id=5059f8619189a5000201fbcb


#2

Oops forgot to enter my code.

puts "Please input"
text = gets.chomp
puts "What is the redact?"
redact= gets.chomp

words= text.split (" ")
words.each do |x|
if x == redact
print "REDACTED "
else
print x + " "
end
end


#3

If there are several words to be redacted, then it would be very cumbersome to have a variable for each one, what you should use instead is a container of some kind to hold them all.


#4

They haven't covered that so far in the lessons.. I'm sure I'll know what you mean eventually :slight_smile:


#5

You've got a container in your code. An array. An array contains other values, it is a contain-er.


#6

I had a feeling you meant that but I'm so green I wasn't sure. I'm hoping someone can show me exactly how I can do this.


#7

How would you do it manually? You'd do the exact same thing in Ruby

Stick the words to redact in an array, then compare each of them to each word in the text.


#8

Ok so I tried to rewriting it

I added: all_redacts= redact.split (" ")
Then I tried to do do this :
if x == all_redacts
When it goes through just prints it as if there are no Reactants even though I will type same words.
Thanks btw

puts "Please input"
text = gets.chomp
puts "What is the redact?"
redact= gets.chomp

words= text.split (" ")

all_redacts= redact.split (" ")

words.each do |x|
if x == all_redacts
print "REDACTED "
else
print x + " "
end
end


#9

Those aren't the same value types, much less equal


#10

I almost got it

puts "Please input"
text = gets.chomp
puts "What is the redact?"
redact= gets.chomp

words= text.split (" ")

all_redacts= redact.split (" ")

words.each do |x|
all_redacts.each do |y|
if x == y
print "REDACTED "
else
print x + " "
end
end
end

Only thing it does now, is if I type two words that come out Redacted. For example First input is Hello how are you. And when it prompts for Redacted, I enter "Hello how" It comes out

Please input
Hello how are you
What is the redact?
Hello how
REDACTED Hello how REDACTED are are you you ["Hello", "how", "are", "you"]

If I type only one it doesn't do this though.


#11

Soo move the print to somewhere that is only executed once per word.

I'm not really providing any insights here, you just need to compare your observations with what you mean to happen, consider the difference therein and make changes accordingly.

If doing this and it has that effect, then what needs to happen differently to get the desired result is ..


#12

It's seems easy to you probably but I just started ruby a few days ago no prior coding experience.

Like I don't even understand why it's executing it multiple times.I'm sure I could get it to work by copy and pasting it somewhere else until I get it but I want to know why it's doing it multiple times in that spot you know?

I do appreciate your help though


#13

The point I'm trying to make is that the things you seem to trip up on don't have anything to do with Ruby. It's a matter of lining up actions to get the desired result, to consider what events you want to take place.

Do you want to print once for each comparison? Or should there be multiple comparisons for each print? When exactly do you want to print? Refer to how you'd be doing this manually. You'd check if it matches any of the words to redact, and based on if there was a match among any of them, you'd print the result.

Forget about Ruby, and just consider what's being done, and what needs to be done differently. You have to figure out which events should take place when.