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 or
ed 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
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.
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.
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'
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 literals in Java
My question is: How can 's', which holds a reference, be compared to the actual string "Hello" which is a character sequence?
"Hello"
is not a character sequence. It's a String
. (String
does implement the CharSequence
interface, but that's not the same thing.) So by the time you're using equals
or ==
, you're comparing String
instances, not some special thing. A String
literal is a String
(complete with a reference to it where the literal was written in the source).
It seemed at first as though you were confused about equals
and ==
. Re-reading, I'm not sure you are, but just in case: equals
compares two objects for equivalence; if the two objects are String
s, it returns true
if they have the same sequence of characters. ==
compares two object references and evaluates true
if they point to the same object. ==
is sometimes true
for what seem to be different String
objects because String
literals are implicitly intern
'd (put in a common pool and reused from it) to save memory, and so two separate but equivalent literals actually do end up referring to the same object. Never rely on that. Using ==
to compare String
s is almost always an error (the cases where it isn't are very much edge-cases). Use equals
.
Expected string literal in condition express in Jenkins pipeline
This error is thrown by the pipeline syntax validator that runs before your pipeline code gets executed. The reason you see this error is the following:
Only "agent none", "agent any" or "agent {...}" are allowed. @ line 6, column 13.
This is the constraint for the label
section. It means that the following values are valid:
agent any
agent none
agent "constant string value here"
agent { ... }
When you pass something like:
agent "${map.agent ?: 'any'}
agent(map.agent ?: 'any')
you are getting Expected string literal
because any form of an expression is not allowed in this place, including interpolated GStrings
of any form.
Solution
There is a way to define pipeline agent dynamically however. All you have to do is to use a closure block with the label
set to either expression or empty string (an equivalent of agent any
in this case.)
pipeline {
agent {
label map.agent ?: ''
}
stages {
...
}
}
The label
section allows you to use any expression, so map.agent
is a valid construction here. Just remember to use an empty string instead of "any"
- otherwise Jenkins will search for a node labeled as "any".
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
What's the meaning of plain string when using as condition?
From http://perldoc.perl.org/perldata.html
Barewords
A word that has no other interpretation in the grammar will be treated
as if it were a quoted string. These are known as "barewords".
Related Topics
How to Count Duplicates Hash Itens in Ruby 1.8.5 ( Sketchup Ruby API )
Flatten a Ruby Array Without Using Built-In 'Flatten' Method
How to Create an Association Between Two Rails Models
Sublime Text Can't Understand Gets.Chomp
How to Handle Combination []+= for Auto-Vivifying Hash in Ruby
Initialize the Delayed Jobs Gem by Starting the Workers on Application Start
How to Send Custom Invitation Using Devise_Invitable
Display Result Data Without Page Refresh on Form Submission in Ruby on Rails
How to Access HTML Elements That Are Rendered in JavaScript Using Xpath
Rails Object Based Permission/Authorization Engine
How to Print All the Staged File Names Using Ruby Git Pre-Commit Hook
Iterate JSON with Ruby and Get a Key,Value in an Array
How to Access Text Field in an Iframe
Should Repeat a Number of Times
Does 'Upcase!' Not Mutate a Variable in Ruby