Warning: String Literal in Condition

warning: string literal in condition

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.

Ruby warning: string literal in condition

Change

if userAgree == 'Y' or 'y'

to

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

Or, cleaner and clearer in my opinion:

if userAgree.upcase() == 'Y'

What does string literal in condition mean?

You have to specify the full condition on both sides of the or.

if response == "a" or response == "A"

The two sides of the or are not connected; Ruby makes no assumptions about what's on the right based on what's on the left. If the right side is the bare string "A", well, anything other than false or nil is considered "true", so the whole expression evaluates as "true" all the time. But Ruby notices that it's a string and not actually a boolean value, suspects you might not have specified what you meant to, and so issues the warning in the question.

You can also use a case expression to make it simpler to do multiple tests against a single value; if you supply a list of multiple possibilities in a single when, they are effectively ored together:

case response
when "a","A"
puts "ok"
when "b","B"
puts "awesome."
else
puts "I'm sorry. I did not get that. Please try again."
end

For the specific situation of ignoring alphabetic case, you could also just convert to either upper or lower before testing:

case response.upcase 
when "A"
puts "ok"
when "B"
puts "awesome."
else
puts "I'm sorry, I did not get that. Please try again."
end

Ruby ternary - warning: string literal in condition

Without parentheses, ruby is interpreting it as

phrase.last.eql?( "?" ? true : false )

which explains the message "warning: string literal in condition".

To fix this, use parentheses on the parameter:

phrase.last.eql?("?") ? true : false

Of course, in this case using the ternary operator is redundant since this is the same as simply

phrase.last.eql?("?")

String literal in condition

A correction to the code especially where you are using OR condition should mute the Warning

if ['Married','MARRIED'].include?(sheet_marital_status)
personnel.marital_status = 'Married'
elsif ['Unmarried','UNMARRIED'].include?(sheet_marital_status)
personnel.marital_status = 'Unmarried'
elsif ['Unknown','UNKNOWN'].include?(sheet_marital_status)
personnel.marital_status = 'Unknown'
else
personnel.marital_status = columns[1].to_s.chomp.strip
end

Because if you use 'XXX' or 'xxx', it always evaluates to 'XXX'. Which means you are comparing sheet_marital_status with only the first string. And that's probably what the compiler warning is indicating. You better use Include.

lemme know your findings too.

Rake: warning: string literal in condition

you get this warning when you evaluate a plain string like after you && because it will ALWAYS be true!

irb(main):003:0> puts "blupp" if "bla"
(irb):3: warning: string literal in condition
blupp
=> nil

Rails if statement using include? method giving either syntax or string literal error

Try:

if self.body.include?('07') || self.body.include?('+447')

I suspect ruby needs the explicit identification of the arguments (i.e., '07' and '+447') provided by the use of parentheses.



Related Topics



Leave a reply



Submit