warning: string literal in condition

后端 未结 4 1393
野性不改
野性不改 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:13

    I was also looking for the answer to this question, and thanks to a friend found a few other solutions.

    1) Change the case for the input so you only have to make one test:

    if input.downcase == "n"
    

    2) Use a more sophisticated check on the input data:

    if %w{n N}.include?(input)     or
    if ['n', 'N'].include?(input)
    

    The second one allows for far more flexibility with your checking , especially if there are groups of entry you are looking for.

    Hope what I found helps others.

提交回复
热议问题