Forum: Ruby (help a beginner) Loop bug

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
22e5646b6066ed88b57700026bc9722b?d=identicon&s=25 Pierre Turnbull (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
from...
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 = Array.new
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 = Hash.new do |hsh, key|
    hsh[key] = []
end

while finished == "no"
    puts "What file would you like to implement?"
    file = gets.chomp
    time = Time.now
    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)
    end

    puts "\nFinished? yes/no"
    finished = gets.chomp
    finished.downcase!
    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
        finished.downcase!
    when finished != "yes" && finished != "no" && finished != ""
        puts "Put \"yes\" or \"no\" please!"
        finished = gets.chomp
        finished.downcase!
    end

end
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (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 != "")
      ...
    end
076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-03-05 05:29
Were you able to determine the solution?
22e5646b6066ed88b57700026bc9722b?d=identicon&s=25 Pierre Turnbull (nounoursnoir)
on 2017-03-05 16:22
Hello,
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
people.

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.