2012-03-23 74 views
1

下面的代碼返回一個錯誤,我不知道爲什麼:未定義的方法錯誤

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」:字符串(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 

回答

0

看起來像一隻流浪逗號而不是一個點:

if element != nil then 
    reg,physicallyPresentInRegistrationCountry = element.text 
#----^^^^ 

這樣做的效果做的多分配兩個VARS regphysicallyPresentInRegistrationCountry和,但只有一個上的=右側表達的意思是

reg = element.text 
physicallyPresentInRegistrationCountry = nil 
+0

看起來就是這樣。謝謝!將接受答案。 – Arjun 2012-03-23 02:51:03

+0

@ user666730不客氣,歡迎來到Stack Overflow – 2012-03-23 02:52:10