Hi. I am having issues with my redacting, I am rying to redact a 2nd word in my string , but its not working. Can anyone spot where I’m going wrong?
many thanks
what are you comparing redact2 to? If you are not comparing it to anything, ruby will simply see if redact2 evaluates to true, which is the case if it is a string
I tried to give you a little nudge in the right direction, then check earlier lessons/documentation if you need a reminder on how to use the or operator
word = "hello world"
if word == "hello" or word == "world"
see? I compare at both sides of or operator, if i do not, the right hand side of the or expression will just check if the variable containing string is true
Rather than arbitrarily seek a fixed number of redacted terms, create an array of terms and write a method that takes that array as a parameter. Then it can be one, two, or more terms passed as a single object to the function.
def redacted(text, words)
text = text.split(' ')
words.each do |word|
text.each do |term|
if term == word
text[text.find_index(term)] = "REDACTED"
end
end
end
text
end
print redacted("the rain in Spain falls mainly in the plains", ['the', 'in'])
We don’t have to use a function, but it is more re-usable. In a one-off situation, the code inside the function can be applied inline. For the two-word example above, simply write the two inputs to an array that can be used by the code. There is no need for || (boolean) or or (control flow) operations.
One should be mindful of the differences between or and ||. For our purposes at this stage of learning, the boolean is the one to reach for. The two are not synonymous.