Ruby警告:条件字符串文字

时间:2012-06-05 11:10:35

标签: ruby warnings

在第七行中,我收到警告“条件字符串文字”。警告意味着什么,我该如何解决?

   print 'Continue to use calculator?   Y or N'
   userAgree = gets.chomp
   if userAgree == 'Y' or 'y'
        userAgree = true
   else
        userAgree = false
   end

4 个答案:

答案 0 :(得分:24)

更改

if userAgree == 'Y' or 'y'

if userAgree == 'Y' or userAgree == 'y'

或者,我认为更清洁,更清晰:

if userAgree.upcase() == 'Y'

答案 1 :(得分:3)

您的if条件无效。这个有效。

print 'Continue to use calculator?   Y or N'
userAgree = gets.chomp
if userAgree == 'Y' or userAgree == 'y'
  userAgree = true
else
  userAgree = false
end

答案 2 :(得分:1)

你可以做到:

    puts 'Continue to use calculator?   Y or N'
    userAgree = gets.chomp
    userAgree.to_s.upcase!

    def Check (ans)

    if ["Y","N"].include?(ans)
        puts"true"
    else
        puts "false"
    end

end

Check userAgree

答案 3 :(得分:-3)

为什么不

if userAgree == ('Y' or 'y')

语法对我来说更具表现力。