FAQ: Redacted! - Great Work!

I’ve tried the below
puts “Enter words to redact (ex: word1, word2):”
redact = gets.chomp.downcase!
redacted = redact.split(", ")

but I’m getting an error in the console: undefined method `split’ for nil:NilClass

Why is that, can’t my delimited for the .split be a comma followed by a space? Or am I missing something?

The bang! method works in-place. It should not be assigned owing to the behavior. If the text has no uppercase characters the assigned value will be Nil. Suggest remove the !.

2 Likes

puts “Input you text:”
text = gets.chomp.downcase!

puts “Input word for redact:”
redact = gets.chomp.downcase!

new_string = “”

words = text.split(" ")
words.each { |word|
if redact.include? word
new_string << "REDACTED "
else
new_string << word + " "
end
}
print new_string

Does you IF statment not need an else?

I tried it BTW and it works great so thanks a lot

I tried the code pictured, but it wasn’t working for me, don’t know why.

So, I went the unless route instead and it worked. Code is below:

puts “Write three words”
text = gets.chomp
words = text.downcase.split (" ")

puts “Two words to censor”
censor = gets.chomp
bad_word = censor.downcase.split (" ")

words.each do |words|
bad_word.each do |bad_word|
unless words != bad_word
words = "CENSORED "
end
end

print words + " "
end

puts "Please input sentence here :"
text = gets.chomp
redact = []
answer = ''

loop do
puts "Please input word to redact :"
redact << gets.chomp
puts "Would you like to add more words? (y/n)"
answer = gets.chomp
break unless answer == 'y' || answer == 'Y'
end

words = text.split (' ')

words2 = []

words.each do |word|
  words2 << word
  redact.each do |redact|
    if word.downcase == redact.downcase
      words2.pop
      words2 << "REDACTED"
    end
  end
end

words2.each do |word|
  print word + ' '
  end

I think all extra points have been included. The words to be redacted are stored in an array which can be as long as the user wants.

puts ‘text please:’
text = gets.chomp

puts ‘words to redacted:’
redact = gets.chomp.downcase!

redacts = redact.split(’ ‘)
words = text.split(’ ')

words.each_with_index do |word, index|
if redacts.include? word.downcase
words.delete(word)
words.insert(index, “REDACTED”)
end
end

puts words.join(" ")

I had this same problem, actually. “execution expired” when I run the code that was given to us as a solution. I’m not really sure why that is and I can’t really troubleshoot as I’ve literally just started learning Ruby yesterday, honestly.

If anyone else has any suggestions, let us know!

Here is what works somewhat.
Click the “Run” button. The Run button will just keep showing like the program hasn’t run. Don’t worry about it. Click in the terminal window with your mouse. Even though the cursor won’t blink, you should immediately start typing your text. You must be quick about it. If you take too long, you will get the “execution expired” message.
In short:

  • Click the “Run” button

  • Click in the terminal window and immediately start typing. You must type your text, hit enter and then type the words to be redacted and hit enter. You must be REALLY quick about this. If you take too long, you will get the execution expired error.

puts "Enter a sentence to redact"
phrase = gets.chomp

puts "Enter words to redact"
redactable = gets.chomp

redactable.split(" ").each do |wordToRedact|
  i = ""
  phrase.split(" ").each do |word|
    if word == wordToRedact
      i += "REDACTED "
    else 
      i += word + " "
    end
  end
  phrase = i
end

print phrase

Hi, in this context what does redact mean?

Thanks

It means to remove from view, similar to making a photo copy of a document and then concealing sensitive or private text with a black felt marker.

1 Like
puts "Input text"
text = gets.chomp
puts "Text to REDACT, separate with commas"
redact = gets.chomp.gsub(/\s+/, "")

words = text.split(" ")
redacted_words = redact.split(",")

output = ''

words.each do |word|
  word_printed = false
  redacted_words.each do |redact_word|
    if word =~ /(?i)\b#{redact_word}\b/
      output += "REDACTED" + " "
      word_printed = true
    end
  end
  if word_printed == false
    output += "#{word}" + " "
  end
end

puts "#{output}"

Managed to get all the bonus things working with this, pretty pleased with myself not going to lie

1 Like

could you please explain it

if word =~ /(?i)\b#{redact_word}\b/

just this line is very complicated

If you haven’t read up on Regular Expressions (regex) and pattern matching, then this would be a good time to explore that topic.

string =~ regex pattern

To be fair, we should at least describe what that pattern is matching to in this example so you have some carry away information. My bad.

We’re looking for a word pattern in a string. We know there won’t be any space characters because we have specified word boundaries with the escape character, '\b'.

As far as the, (?i), that has something to do with a capturing group which I might have simply made into a greedy match. Ask me more about this when you are more aware of Regex and we might both learn something.

Realize that Regex is another language that is very strict in how it interprets the patterns we give it to match against.

I try this, amazing!!! :ok_hand: , is not perfect but its working

redactor = []   # array for redact to input many values
result = ""  #display result

puts "Write anything : "
text_saisie = gets.chomp 

loop do    # start loop for add many words to redact 
    puts "Word to be change ? "
    redactor << gets.chomp    #add value in redactor

    puts "Will you enter another word ? y or n "
    user_input = gets.chomp
    user_input.downcase!
    while (user_input.empty?) || (user_input != "y" && user_input != "n")   #this loop forces the user to answer with y or n
      puts "Please enter y or n only!!"
      user_input = gets.chomp
      user_input.downcase!
    end
    break if user_input == "n"      # if the answer is yes the loop is restart
end

words = text_saisie.split(" ")  # This separated input in the beginning of this program

words.each do |word|              #iteration 
  if redactor.include? word       #matching between the two lists 
    word = "REDACTED "
  else 
    word = word + " "
  end
    result += word         # the result is saved in result
end 
print result   

any idea why i’m getting this error?

Line 9 looks a bit off. Should it be singular?