Trying to redact a second word from string (with great difficulty)


#1

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

puts "Enter some text: "
text = gets.chomp
text.capitalize!

puts "Enter word to redact: "
redact = gets.chomp
redact.capitalize!

puts "Enter 2nd word to redact: "
redact2 = gets.chomp
redact2.capitalize!

words = text.split(" ")
words.each { |word|
if word == redact; redact2
print "REDACTED "
else
print word + " "
end }


#2

how is your comparison working on line 15? I haven’t done ruby in a while, but i would say you need the or operator


#3

Hi, thanks for your swift reply. I have tried ‘or’ in line 15, but this results in all text being redacted (as below)


#4

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


#5

Hi
I’m brand new to all this, but managed to get it to work with an elsif. I’m still very unclear on operators, back to the books!
thanks/db

words = text.split(" ")
words.each { |word|
if word == redact
print "REDACTED "
elsif word == redact2
print "REDACTED "
else
print word + " "
end }


#6

or would be better, you just need to comparison at both sides of the or operator.


#7

Sorry, are you able to elaborate, i have tried about 20 diff options and am still unable to get it to work.
thanks/db


#8

for example:

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


#9

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'])
["REDACTED", "rain", "REDACTED", "Spain", "falls", "mainly", "REDACTED", "REDACTED", "plains"]
puts
puts redacted("the rain in Spain falls mainly in the plains", ['the', 'in']).join(' ')
REDACTED rain REDACTED Spain falls mainly REDACTED REDACTED plains

Redact a list

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.

Using “and” and “or” in Ruby


#10

Thanks you both! I was really not getting that.
Thanks for the 2 levels of solution, I will def remember those going fwd in my endeavours
cheers/db


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.