Ruby TypeError: no implicit conversion of String into Integer
Ruby TypeError: no implicit conversion of String into Integer
我正在尝试查找我的 data
中是否有 mango/cc:
,如果它在那里更新它的值到 new_version
,如果它不在那里添加 mango/cc:#{new_version}
我的data
{"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
代码:
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details'] << "mango/cc:#{new_version}"
else
data['details'].delete(var1)
data['details'] << "mango/cc:#{new_version}"
puts data
当前输出:
`[]': no implicit conversion of String into Integer (TypeError)
预期输出:
{"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_53.0.0"]}}
我认为错误是因为 var1 是一个数组,所以你的代码必须是这样的:
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details'] << "mango/cc:#{new_version}"
else
data['details'].delete(var1.first)
data['details'] << "mango/cc:#{new_version}"
puts data
只是更改 data['details'].delete
方法中的参数
所以它应该有效:
data={"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
puts "Before: #{data}"
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details']['SS'] << "mango/cc:#{new_version}"
else
data['details']['SS'].delete(var1.first)
data['details']['SS'] << "mango/cc:#{new_version}"
end
puts "After: #{data}"
#=> Before: {"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
#=> After: {"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_53.0.0"]}}
有几个错误:
var1 is an array
data ['details'] is hash table
"mango / cc: # {new_version}" is a string
您应该添加 ['SS'] 以删除键(字符串)"mango / cc: # {new_version}" 记住 var1 是数组而不是字符串
我正在尝试查找我的 data
中是否有 mango/cc:
,如果它在那里更新它的值到 new_version
,如果它不在那里添加 mango/cc:#{new_version}
我的data
{"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
代码:
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details'] << "mango/cc:#{new_version}"
else
data['details'].delete(var1)
data['details'] << "mango/cc:#{new_version}"
puts data
当前输出:
`[]': no implicit conversion of String into Integer (TypeError)
预期输出:
{"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_53.0.0"]}}
我认为错误是因为 var1 是一个数组,所以你的代码必须是这样的:
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details'] << "mango/cc:#{new_version}"
else
data['details'].delete(var1.first)
data['details'] << "mango/cc:#{new_version}"
puts data
只是更改 data['details'].delete
方法中的参数
所以它应该有效:
data={"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
puts "Before: #{data}"
new_version = "287_457_53.0.0"
var1 = data['details']['SS'].select{|x| x.start_with?('mango/cc:')}
if var1.empty?
data['details']['SS'] << "mango/cc:#{new_version}"
else
data['details']['SS'].delete(var1.first)
data['details']['SS'] << "mango/cc:#{new_version}"
end
puts "After: #{data}"
#=> Before: {"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_51.0.0"]}}
#=> After: {"product"=>"fruit", "id"=>"alpha", "details"=>{"SS"=>["mango/aa:50", "mango/cc:287_457_53.0.0"]}}
有几个错误:
var1 is an array
data ['details'] is hash table
"mango / cc: # {new_version}" is a string
您应该添加 ['SS'] 以删除键(字符串)"mango / cc: # {new_version}" 记住 var1 是数组而不是字符串