REDACTED does not work for multiple occurrence of the word, Any idea?


#1

I have uploaded my code and testing. Please explain why it is not working ?
Thank you.

It does not even redacted first occurrence of the word.

Below is the code

puts 'Please enter the text:'
text = gets.chomp

puts 'Please enter the redact:'
redact = gets.chomp
redact = redact.downcase!

words = text.split(" ")

words.each do |word|
    if word == redact
        print "REDACTED "
    else
        print word + " "
    end
end

#2

I've written code to REDACT multiple occurrence in a text. There is something issue with the code. Can someone help me out ?

Thank you


#3

Your second example doesn't print `REDACTED'. A slight twist on your method...

words.each do |word|
  if word == redact
    word.gsub!(redact,'REDACTED')
  end
  print word + " "
end

Your first example works fine. I didn't use the line,

redact = redact.downcase!

because if your typed in word (in the example you use hash) is all lowercase, this line will assign nil to redact, which is why your code didn't work. There is no 'nil' in your text.


#4

Great, Thanks!!.
It worked. However, can you please explain why it didn't work when I put space at the end of the word 'REDACTED' ??

Thank you once again.


#5

The code (in the first example) works correctly when the downcase! line is removed.


#6

Yes, Thank. That issue I solved after posting.
Thank you