[英]String literal in condition
I'm trying to insert some bulk of Data to a Particular Column in my Table by using CSV file format. 我正在尝试使用CSV文件格式将大量数据插入表格中的特定列。 My code is as follows: 我的代码如下:
Code : 代码 :
def maritalstatus_migration
filename = "#{RAILS_ROOT}/config/MaritalStatus_Data_Migration_240114.csv"
file=File.new(filename,"r")
while (line = file.gets)
columns = line.split("$")
employee = Employee.find_by_employeeid(columns[0].to_s.chomp.strip)
personnel = Personnel.find_by_extid(columns[0].to_s.chomp.strip)
if employee && personnel
sheet_marital_status = columns[1].to_s.chomp.strip
if sheet_marital_status == 'Married' or 'MARRIED'
personnel.marital_status = 'Married'
elsif sheet_marital_status == 'Unmarried' or 'UNMARRIED'
personnel.marital_status = 'Unmarried'
elsif sheet.marital_status ='Unknown' or 'UNKNOWN'
personnel.marital_status = 'Unknown'
else
personnel.marital_status = columns[1].to_s.chomp.strip
end
end
end
end
When I run my method in Console,I get a Warning saying: 当我在Console中运行我的方法时,我得到一个警告说:
String literal in condition
pointing to the line personnel.marital_status = columns[1].to_s.chomp.strip
, what am i doing wrong.Any Suggestions will be greatly appreciated. 指向行personnel.marital_status = columns[1].to_s.chomp.strip
,我做错了什么。任何建议将不胜感激。
A correction to the code especially where you are using OR condition should mute the Warning 对代码进行更正,尤其是在使用OR条件的情况下,应将警告静音
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'. 因为如果你使用'XXX' or 'xxx'
,它总是评估为'XXX'。 Which means you are comparing sheet_marital_status with only the first string. 这意味着您只将sheet_marital_status与第一个字符串进行比较。 And that's probably what the compiler warning is indicating. 这可能是编译器警告指示的内容。 You better use Include. 你最好使用Include。
lemme know your findings too. lemme也知道你的发现。
I'd use a case
statement: 我会使用一个case
陈述:
personnel.marital_status = case columns[1].to_s.chomp.strip
when 'Married', 'MARRIED'
'Married'
when 'Unmarried', 'UNMARRIED'
'Unmarried'
when 'Unknown', 'UNKNOWN'
'Unknown'
else
columns[1].to_s.chomp.strip
end
The String literal in condition
warning is shewn up, when you have passed the instance of String
class as a condition for if
operator, ie for example, x or y
, where x
is boolean (correct), and y
is String
(incorrect). 当你传递String
类的实例作为if
运算符的条件if
,例如x or y
,其中x
是布尔值 (正确), y
是String
(不正确), String literal in condition
警告中的String literal in condition
就会被修改。 Let's see the behaviour of the condition that leads to the warning: 让我们看看导致警告的条件的行为:
if false or "ww12"
p 1
end
# warning: string literal in condition
1
=> 1
As you can see, the string literal condition is always evaluated to true
. 如您所见, 字符串文字条件始终计算为true
。 So for most case it could be treated as a syntax error. 因此,对于大多数情况,它可以被视为语法错误。
In order to fix it just convert the String
into boolean , like x == 'string'
. 为了修复它,只需将String
转换为boolean ,就像x == 'string'
。 And for your code you will get: 对于您的代码,您将获得:
if sheet_marital_status == 'Married' or sheet_marital_status == 'MARRIED'
...
end
or with optimizations: 或者通过优化:
if sheet_marital_status =~ /^married$/i
...
end
NOTE: for your case, it will be best to use you case/when
statement, instead of the if-elsif-else
statement, because you have homogenic condition in the tree. 注意:对于您的情况,最好使用case/when
语句,而不是if-elsif-else
语句,因为您在树中具有同源条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.