Redacted 6/6 Understanding Problem



After smashing my head against the screen for awhile I finally got this working after looking through a post in ~Oct '15. Problem is I don’t know why this works and my original one doesn’t.

puts "Please provide the text to search: "
text = gets.chomp
puts "Please type the 3 words you'd like redacted: "
word_a = gets.chomp
word_b = gets.chomp
word_c = gets.chomp
redact = [word_a, word_b, word_c]

words = text.split(" ")

words.each do |word|
  if redact.include?(word)
    print "REDACTED "
    print word + " "

my if statement of
if redact.include?(word)
works but
if word.include?(redact)"
doesn’t work. Looking at it, the second makes more sense because it’s scanning the all the text (converted into individual strings held in an array) for any strings that match the array of strings in redact. When used I get an error that says cannot covert Array into String.

The one that works, scans word against redact instead of redact against word. I feel you should be checking the smaller list against the bigger list rather than the other way around.

Does it work because words.each do |word| sets the parent method the others are encased in? Does it have to do with how they were made; redact being assigned as variables in the array vs being converted from 1 string? Is it because I’m searching each individual word in words to see if they’re included on the redact array?

Any help would be appreciated.

Link that helped me solve it in the first place.

redact = []
3.times {redact << gets.chomp}

An array may include a word, but a word will not include an array.


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