Control flow know how


#1

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

not sure what im doing wrong can someone assist me


#2

You'll need to start by considering what the incorrect behaviour is! If you agree that that behaviour is wrong, then what is responsible for that part of the behaviour? And then look closer at that part, perhaps adding prints to observe what it's doing.

I imagine that you've already done the first part of that, but since you haven't said what's stopping you from doing the rest, I'm not sure what you're missing? What's stopping you, what information do you need? Ask questions!


#3

im not sure what im missing or which part of my code is wrong ive been staring at it for an hour and cant figure it out. im not sure where the incorrect behavior is and have had no issues with this exercise until i reached this step


#4

But you've got something that indicates that it is wrong, right? That's where you have to start. If you can't tell from that something what the incorrect behaviour is, then you might ask about what it's trying to tell you.


#5

(ruby):13: syntax error, unexpected $end, expecting keyword_end

Oops, try again. Your syntax doesn't look quite right--check the Hint if you need help.

those are the errors it gives me


#6

That says quite a lot, and error messages like that are very valuable (otherwise they'd just be silent and say it crashed)

It reports that at line 13 there is a problem with the syntax. keyword_end refers to the end keywords that you put after methods, loops, classes etc. And $end refers to end of file. With that you should be able to make sense out of that error message. Error messages are also usually very good search queries if you don't understand them, somebody else is likely to have asked about that same error message.

A somewhat newer ruby-version would phrase it this way instead:

14: syntax error, unexpected end-of-input, expecting keyword_end

#7

i seemed to have fixed it by adding end to line 14 but i dont understand why it is expecting the end keyword twice at the end of the if else statement ive passed the exercise but i wanna make sure i fully understand what is happening and why it is necessary to do it this way


#8

When is end used?
Did you use it all those times before?

Perhaps you've indented your code in a way that doesn't match its meaning, and then you're looking at the indentation to determine how many ends there should be? (I can't tell though, since your code isn't entirely intact, it's rather important to make sure that you send the very same sequence of characters to other people when sharing code, as what you've got.)


#9

i see now what is happening and why thank you for your assistance btw


#10

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