如何纠正一个'条件#39;中的字符串文字;红宝石警告?

时间:2014-04-22 15:26:57

标签: ruby

def cafeteria 
  puts "The cafeteria is a mess, as you walk into the kitchen all the pots and pans are all      over the floor."
  puts " The freever is open, everything has spoiled, and it smells awful!"
  puts "At the far end of the kitchen a set of knives is lying on the floor."
  puts "What do you do?"
  prompt()
  action = gets.chomp                           
  if action.include? 'knife' or 'knives'
    puts "Yes! now you have a weapon."
    puts "Now what?"
  elsif action == "leave."
    return :hallway
  else 
    puts "I don't understand."
    return :death
  end
end

1 个答案:

答案 0 :(得分:1)

action.include? 'knife' or 'knives'将始终产生真值。 (即使字符串中没有kinfeknives

action = 'asdf'
# => "asdf"
action.include? 'knife' or 'knives'
# => "knives"

因为它被解释为(action.include? 'knife') or 'knives'

也许你的意思是关注?

action.include? 'knife' or action.include? 'knives'
# => false