6. Great Work! *Supplemental Questions*


#1

Hi!

My name is Jonathan, and I was wondering if there are solutions out there for the following questions provided from the final lesson!

  • What could you do to make sure your redactor redacts a word regardless of whether it's upper case or lower case?
  • How could you make your program take multiple, separate words to REDACT?
  • How might you make a new redacted string and save it as a variable, rather than just printing it to the console?

I really want to get better at Ruby, and I want to milk every inch of these lessons so I can get better! Thanks for the much needed help! :smiley:


#2

1) Convert to all lower case or all upper case when comparing
2) Isn't it already doing that?
3) Put the words in an array, concatenate them together when done looping


#3

Hey @valormoreghoulis Jonathan! I'm curious about these as well.

@ionatan Yes, No, and Yes.

Below is the code I came up with for first and last questions:

puts "Enter the non-redacted text please:"
text = gets.chomp
puts "Enter the word you'd like to redact please:"
redact = gets.chomp
final = []

words = text.split (" ")

words.each do |x|
    if x.downcase == redact.downcase
        final.insert(-1, "REDACTED ")
    else
        final.insert(-1, "#{x} ")
    end
end

final.each {|x| print x}
puts ""

To explain: To solved the first question, I simply added. if x.downcase == redact.downcase, which got all the text comparison on an even level, but retained the cases on the final output.

Solving the third question was slightly trickier. First, I made sure to define a variable final with nothing in it. If I didn't put " = [] it wouldn't spit out what I wanted. Same if I didn't define it at all.

This all took a little bit of extra research, which I'm sure was their intention. This website was quite useful. Read that to see how the .insert method works, and how I used that to put my redacted text into it's own variable.

I'm still having trouble with the second question. I've used stackexchange.com to try to see how one might compare separate elements in two arrays. Still working on it...

Anyone have any ideas?


#4

Ah yes! Using @justinbernard's boolean trick I got questions 1, 2 and 3 satisfied now! Thanks @justinbernard!

puts "Enter the non-redacted text please:"
text = gets.chomp
puts "Enter the word(s) you'd like to redact please:"
redact = gets.chomp
final = []
set = false

words = text.split (" ")
noShow = redact.split (" ")

words.each do |x|
    noShow.each do |q|
        if x.downcase == q.downcase
        final.insert(-1, "REDACTED ")
        set = true
        end
    end
final.insert(-1, "#{x} ") unless set
set = false
end

final.each {|x| print x}
puts ""

#5

The words are separated by space. If they are separated by something else, if they are already in an array, then don't use split? I'm not seeing how that's not exactly what's happening in the exercise already


#6

Here's my version. I took the redacted string and converted it to an array as well. Then used the .include? method to search the array. Then the .insert method to put the results into the new array "final". printed each item from that array at the end just to make sure it worked.

puts "Enter your text to search."
text = gets.chomp
text.downcase!

puts "Enter the text you wish to redact."
redact = gets.chomp
redact.downcase!

final = []

red_str = redact.split " "

words = text.split " "

words.each do |x|
if red_str.include?(x)
final.insert(-1, "REDACTED " )
else final.insert(-1, x + " " )
end
end

final.each { |x| print x}


#7

it is much easier to deal with string data. Because otherwise I have no idea how to solve multiple redacted_words (several)

puts "Give me your text"
text=gets.chomp
words = text.split(" ")
redact=""

while redact=="" do
print "Word(s) to redact - "
redact=gets.chomp
end

text_redacted=text.gsub redact, "REDACTED"
puts "Redacted text\n#{text_redacted}"
puts "Redacted - #{redact}"


#8

Nice work. I’ve tried to make a slightly cleaner version and changed some of the names for clarity. By adding the " " into the .each loop at the end you remove the need for a space in "REDACTED ". You also don’t need the set = false at the top because it will only check if it is true which it will not be if it doesn’t exist.

puts "What is the text you want to alter?"
text = gets.chomp
puts "What are the word(s) you want redacted?"
redact = gets.chomp
final = []

words = text.split (" ")
redact_words = redact.split (" ")

words.each { |word|
  redact_words.each { |redact_word|
    if word.downcase == redact_word.downcase
    final.insert(-1, "REDACTED")
    redacted = true
    end
    }
  final.insert(-1, word) unless redacted
  redacted = false
}

final.each { |word| print word + " "}