warning: string literal in condition

后端 未结 4 1398
野性不改
野性不改 2021-02-05 08:49

Using the first bit of code below I receive two warning messages: warning: string literal in condition x2

if input == \"N\" || \"n\"
  #do this
else         


        
4条回答
  •  暖寄归人
    2021-02-05 09:20

    When you are writing input == "N" || "n"( internally Ruby sees it (input == "N") || "n"), it means "n" string object is always a truth value. Because in Ruby every object is true, except nil and false. Ruby interpreter is warned you there is not point to put ever true value is conditional checking. Conditional check statement always expect equality/un-equality test kind of expression. Now you can go ahead this way or re-think again. if input == "N" || input == "n" is not throwing any warning, as it obeys the norm of conditional test.

    else input == "L" || "l" is wrong, as else statement don't expect any conditional test expression. Change it to elseif input == "L" || "l"

提交回复
热议问题