Chris Pine学习编程Ruby Ch 9:心理测试计划

时间:2012-01-05 19:40:56

标签: ruby-on-rails ruby methods

所以我跟着并在Chris Pine做了心理测试[burrito,taco,wets_bed]节目......是的,我换成了“Escape:The Pina Colada song”的歌词,但不然,保持不变。

然而,它仍然停留在第一个“问”上。 。 。帮助

我不想彻底改变这种情况,只是试图找到程序挂起的位置。

# 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版本的问题?

谢谢!

3 个答案:

答案 0 :(得分:6)

这条线路有问题吗? good_answer == true

不应该是good_answer = true

答案 1 :(得分:1)

如果你打开了警告,Ruby本身就会告诉你good_answer == true行没有意义。

您可以在代码中使用$VERBOSE = true打开警告,或者让unix变量RUBYOPT包含-w

答案 2 :(得分:0)

顺便说一句,我会像这样重写ask函数,以便更符合Ruby方式:

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