Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 441 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
warning: string literal in condition

#1


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 input == "L" || "l"
#do this

as opposed to using this which results in no warnings

if input == "N" || input == "n"
#do this
else input == "L" || input == "l"
#do this

I'm wondering why the first bit of code results in a warning, and the downside of using it.
Reply

#2
change `input == "N" || "n"`

to

input == "N" || input == "n"

You must also use `else if` instead of `else`


**The warning is saying that instead of a boolean or test, you have a string literal, ' n', which always evaluates to true.**

Reply

#3
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.
Reply

#4
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"`
Reply

#5
I have the same error as you but a different problem, found this through Google might as well post my solution for the next Googler.

I had a typo in my code which also gives the same warning:

if input =! "N"

of course the right way:

if input != "N"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through