Ruby warning: string literal in condition
10,130
Solution 1
Change
if userAgree == 'Y' or 'y'
to
if userAgree == 'Y' or userAgree == 'y'
Or, cleaner and clearer in my opinion:
if userAgree.upcase() == 'Y'
Solution 2
Your if
condition is not valid. This one works.
print 'Continue to use calculator? Y or N'
userAgree = gets.chomp
if userAgree == 'Y' or userAgree == 'y'
userAgree = true
else
userAgree = false
end
Solution 3
you can make that:
puts 'Continue to use calculator? Y or N'
userAgree = gets.chomp
userAgree.to_s.upcase!
def Check (ans)
if ["Y","N"].include?(ans)
puts"true"
else
puts "false"
end
end
Check userAgree
Author by
Russian Street Bear
Updated on August 24, 2022Comments
-
Russian Street Bear almost 2 years
In the seventh line, I get the warning, "string literal in condition". What does the warning mean, and how can I resolve it?
print 'Continue to use calculator? Y or N' userAgree = gets.chomp if userAgree == 'Y' or 'y' userAgree = true else userAgree = false end