![](/img/trans.png)
[英]Ruby undefined method `[]' for nil:NilClass (NoMethodError) error
[英]undefined method error in ruby NoMethodError
以下代码返回错误,但我不知道为什么:
require "rexml/document"
include REXML
file = File.new("test.xml")
doc = REXML::Document.new file
class Registration
attr_accessor :number, :jurisdiction, :physicallyPresentInRegistrationCountry
end
def constructRegistration(item, typeOfMerchant)
reg = Registration.new
element = item.elements[typeOfMerchant + "PhysicallyPresentInRegistrationCountry"]
if element != nil then
reg,physicallyPresentInRegistrationCountry = element.text
else
reg.physicallyPresentInRegistrationCountry = nil
end
return reg
end
XPath.each(doc, "//transactionAuditRecordList/item") { |item|
reg = constructRegistration(item, "seller")
puts reg.physicallyPresentInRegistrationCountry
}
rexml.rb:26:未定义的方法“ physicallyPresentInRegistrationCountry”为“ false”:字符串(NoMethodError)
from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
from rexml.rb:24
看起来像是一个逗号,而不是一个点:
if element != nil then
reg,physicallyPresentInRegistrationCountry = element.text
#----^^^^
这具有对两个vars reg
和physicallyPresentInRegistrationCountry
进行多重分配的效果,但是=
右侧的一个表达式表示
reg = element.text
physicallyPresentInRegistrationCountry = nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.