Forum: Ruby (help a beginner) Loop bug

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
849a59f2cec846fdb87df6bf9daf6ec8?d=identicon&s=25 Pierre T. (nounoursnoir)
on 2017-01-21 17:07
Attachment: aaruby.png (40 KB)
hello world!
i'm a beginner at Ruby language, and i'm facing a problem for which i
fail to find the solution.

I will put a big block of code since I don't know where the error comes
The problem is that when I answer something else than "yes" or "no" to
the question "Finished?", the program stops the while loop and goes to
the next block of code. Instead of stopping it, it should ask me again
to put "yes" or "no", until I put "yes" or "no".

I also join a screenshot of my code so you can see it better. (note: in
this post, i've deleted some useless part of code and traducted some
comments from french to english)

Here is my code:

list =
finished = "no"
#create a hash in which the values are lists of values, so i can have
keywords corresponding to authors, and lists of values corresponding to
the lists of files created by each author
hash = do |hsh, key|
    hsh[key] = []

while finished == "no"
    puts "What file would you like to implement?"
    file = gets.chomp
    time =
    puts "Who's the author?"
    author = gets.chomp

    if hash[author].include? file
        puts "There already is a file named \"#{file}\" corresponding to
the author \"#{author}\"."
    #gives a value to the value-list of a key
    else hash[author].push(file)

    puts "\nFinished? yes/no"
    finished = gets.chomp
    puts ""

    #here, whenever i give the variable finished another value than
"yes" or "no", it should ask again the user to put a value in the
variable finished, until the value given is "yes" or "no"
    case finished
    when finished == ""
        finished = gets.chomp
    when finished != "yes" && finished != "no" && finished != ""
        puts "Put \"yes\" or \"no\" please!"
        finished = gets.chomp

3a81ced4012650ca2004c9bff7b18202?d=identicon&s=25 Ronald F. (rovf)
on 2017-01-23 14:24
Your case statement doesn't make sense. You compare a String (finished)
to the result of a logical expression, i.e. true or false. Basically,
your case block is equivalent to

    if finished === (finished == "")
    elsif finished === (finished != "yes" && finished != "no" &&
finished != "")
90852f256316413e0ca534c87e8026c8?d=identicon&s=25 GLENN C. (rubynewb)
on 2017-03-05 05:29
Were you able to determine the solution?
849a59f2cec846fdb87df6bf9daf6ec8?d=identicon&s=25 Pierre T. (nounoursnoir)
on 2017-03-05 16:22
sorry I haven't seen the answer, and have forgotten this post since I
found the answer later by experimenting and getting help from some

So, yes I had found a solution.
If my memory is good, the point was to put a simple loop with some "if"
statement to break the loop, instead of using a while loop. Simpler, and
it works fine. About the case statement, I remember that I understood
why it didn't make sense, and replaced it with a simpler case statement,
inside of a loop that would break when a condition is met.
This topic is locked and can not be replied to.