Control Flow Know-How REDACTED!


#1

I just have a quick question about the END. Why does this code require you enter END twice in order for it to work? I couldn't get it to work until I saw it on another post but I have no idea why it requires END twice.

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

words = text.split ("")
words.each do |words|
    if words == redact
    print "REDACTED "
else 
    print words + " "
end
end


#2

The first end is needed to close the if else construction. Second end closes the each block.


Short advice - do not use the same name for the object and for the iterative variable (words). For the iterative variable use word, this is what it represents, right?


#3

When you type ["words.each do ..."] the computer sees that is kinda of the "Block" , so you have to end it. then inside this "Block" what you did is you typed ["if words==redact... ..else print words..."] thats another block so you also have to end it. two blocks, two ends?

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 "REDACTED "
else
print "#{word} "
end
end

block inside a block


#4

I thought I was suppose to use word for both. I'll have to review it again. Thanks!


#5

that makes sense, thank you!


#6

then inside this loop what you did is you typed ["if words==redact... ..else print words..."] thats another loop

if construction is not a loop. Loop implies that block of code will be executed many times.


#7

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