mardi 11 octobre 2016

Selecting a single element from an array, within an "if" statement

Creating a method that receives input from a user -- and if said input includes any word from a predetermined array, it prints the word "success!" Otherwise, if the input doesn't include a word from that array, it prints the word "failure."

My ideal would be to create a scenario where I can populate the array with as many elements as I want, and the method will always reference that array when deciding how to react to the user's input.

When I run it, however, I get an error message saying "no implicit conversion of Array into String."

My code is below. Appreciate any and all help! Thanks.

            def hello
            word_bank = ["hello", "hi", "howdy"]
            print "Say a word: "
            greeting = $stdin.gets.chomp.to_s

            if greeting.include?(word_bank[0..2])
               puts "success!"
              else
                puts "failure."
              end

            end

            hello

Aucun commentaire:

Enregistrer un commentaire