2012-01-05 9 views
0

私はクリスパインのPsych Test [burrito、taco、wets_bed]プログラムに従っています...はい、私は "Escape :ピナ・コラダの歌 "それ以外は、同じままにしていた。クリスパインRubyをプログラムする方法を教えてください。Ch 9:Psychテストプログラム

しかし、最初の「尋ねる」にはまっています。 。 。助けて?

私はこれを大幅に変更したくなく、プログラムがハングしている場所を見つけようとしています。

# Nice little questionnaire 

def ask question 
    good_answer = false 
    while (not good_answer) 
    puts question 
    reply = gets.chomp.downcase 

    if (reply == 'yes' or reply == 'no') 
     good_answer == true 
     if reply == 'yes' 
     answer = true 
     else 
     answer = false 
     end 
    else 
     puts 'Please answer "yes" or "no".' 
    end 
    end 

    answer # This is what we return (true or false) 
end 

puts 'Hello, and thank you for smoking.' 

puts 

ask 'Do you like pina-coladas?' 
ask 'Do you like getting caught in the rain?' 
risky_business = ask 'Do you know what your sig other likes?' 
ask 'Are you into yoga?' 
ask 'Do you have half a brain?' 
puts 'Just a few more questions.' 
ask 'Do like the feel of the ocean?' 
ask 'Are you into champagne?' 

puts 
puts 'DEBRIEFING' 
puts 'Thank\'s for all the fish' 
puts 
puts risky_business 

これだけです。 これがRubyのバージョンの問題である可能性がありますか?

ありがとうございます!

答えて

6

この行に問題がありますか?

def ask(question) 
    puts question 

    while reply = gets.chomp.downcase 
    if %w(yes no).include?(reply) 
     answer = if reply == 'yes' then true else false end 
     break 
    else 
     puts 'Please answer "yes" or "no".' 
    end 
    end 

    answer 
end 
+0

AH!ありがとうございました...私はそれがそういう感じだった。 – agentfin

0

する必要がありますそれ自体はgood_answer == true行が意味をなさないことを伝えます。

コード内の$VERBOSE = trueで警告を有効にするか、unix変数RUBYOPT-wが含まれるようにすることで警告をオンにすることができます。

+0

これは知っておいて良いですが、それはChris Pineの本のこのセクションで起こっていることを超えています。 – agentfin

1

あなたは警告がオンになっていた場合、ルビー:good_answer == true

はない、それは私がRubyの道に沿って、よりをしたい機能を尋ねる書き換えるだろう、ところでgood_answer = true

関連する問題