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 !.
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 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.
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
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
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!!! , 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