Lesson 4 all things get redacted


#1



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


my code does redact all the words and i wonder why. it should only redact one word.


puts "Gib einen Text ein: "
text = gets.chomp
puts "Text to redact: "
redact = gets.chomp

words = text.split(" ")

words.each do |word|
    if word = redact
        print "REDACTED "
    else 
        print word + " "
    end
end


#2

@flightofthepenguin ,

Is this intended as a comparison or as an assignment? ...

if word = redact

#3

don't know what you mean. the original code at the start of the lesson is this:
as you see i only swapped the != redact

puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp

words = text.split(" ")

words.each do |word|
if word != redact
print word + " "
else
print "REDACTED "
end
end


#4

You have the = assignment operator in the if block header. You need to use the == comparison operator there.


#5

first i wanted to complain about your comment(because i thought that couldn't be the mistake) but you are absolutely right :slight_smile:
Thanks a lot. that still gives me a headache but i think i get to it now.


#6

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