6,6 - Trying to branch out with poor results!


I've been playing around with the code for this lesson but, for some reason, I can't seem to make the last 'if' statement call the second array that I've created from the two pieces of redacted text.

puts "Please enter your text here: "
text = gets.chomp

puts "Please enter a word to redact: "
redact = gets.chomp

puts "Would you like to enter another word to redact? (yes/no)"
ans = gets.chomp

if ans == "yes"
puts "Please enter your second word to redact: "
redact2 = gets.chomp
puts "Fair enough..."

words = text.split(" ")

redacts = [redact,redact2]

words.each do |word|
if word != redacts
print word + " "
print "REDACTED "

Just what is it that I'm doing wrong, here? Is it something to do with how I'm referencing the second array? Is that not possible within an 'if' statement?



From what I understand, your redacts array is now composed of two sub-arrays. Meaning that if word != reacts only access the 1st layer of the array, not the 2nd layer.

How to change that, I am not sure yet but it might be the origin of the problem


it's because you are testing if a string (an element within an array) is equal to an array. 'word' will never equal redacts in that case. Rather, you want to check if 'word' is equal to one of the VALUES WITHIN THE ARRAY. It doesn't work simply by testing if a string is equal to an array. Instead, you'll need to iterate through each element within 'redacts', or use another method.

try this instead:

if redacts.include? (word) == false

This line will check if 'word' is found in the array called redacts.