REDACT multiple words


After finishing the redact exercise, (6/6) I decided to try the second thought question: How could you make your program take multiple, separate words to REDACT?

My error happens here:

For example here, I typed the phrase: "His name is Jim Jake", and I tried to redact "Jim Jake" and this error appears:

His His name name is is REDACTED Jim Jake REDACTED ["His", "name", "is", "Jim", "Jake"]

I was wondering what was wrong with my code, or if anyone has the proper code for how to redact multiple words properly. Thanks! (See code below)

puts "Type text:"
text = gets.chomp
puts "Redact:"
redact = gets.chomp

words = text.split(" ")
redact_words = redact.split(" ")

words.each do |x|
  redact_words.each do |y|
      if x == y
        print "REDACTED "
        print x + " "


You iterate through each word in the text. Say you have 8 words, that's 8 iterations.

In each such iteration, you iterate through each word to redact. Say there are two, then you have a total of 16 inner iterations.

In each such inner iteration you print something, so that for a text of 8 words and 2 words to redact, you print 16 times.

If the problem is that you print once for each word to redact where you should only print once, then you have also at the same time said what the solution is, if you just re-order the words of that a little. All you need to do is to consider what events need to take place for each word in the text, that's something that isn't related to ruby itself, just order of events, you can start by writing those events in English and do the translation once you've got that sorted out.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.