6/6 Multiple redacts using commas?


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/6?curriculum_id=5059f8619189a5000201fbcb

After reading lots of posts on the forum, I've tried to optimize my code and this works best so far. However, when multiple words are keyed in with commas in between, such as:

Only the first word is redacted, in this case, "like":

Below is my code:

puts "Say something?"
text = gets.chomp
 text.downcase!

puts "Which words shall I hide? Separate each with a comma."
redact = gets.chomp
 redact.downcase!

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

words.each do |word|

    if redacted_words.include? word
        print "REDACTED " 
    else
        print word + " "
    end
end


I look forward to all your help. Thank you! :smile:


#2

@onehungrycat,

If you want to replace a string consisting of multiple words
you will end up, using

text="This is this is a test text this"
the_str = "this is"
new_contents = text.gsub(/#{the_str}/, "replacement string")