私の質問は、nokogiriを使用しているhappymapperのフォークであるhttps://github.com/dam5s/happymapperのドキュメントに基づいて出力を作成することに関連しています。Happymapper(fork) - 複数のクラスからの出力
私はドキュメンテーションで2つの例を使用しています。これは私の例です。
xml_doc = <<EOF
<address location='home'>
<street>Milchstrasse</street>
<street>Another Street</street>
<housenumber>23</housenumber>
<postcode>26131</postcode>
<city>Oldenburg</city>
<country code="de">Germany</country>
</address>
EOF
class Address
include HappyMapper
tag 'address'
element :housenumber, Integer, :tag => "housenumber"
end
class Country
include HappyMapper
tag 'country'
attribute :code, String
content :name, String
end
outputs = Country.parse(xml_doc)
outputs.each do |output|
puts output.code
puts output.name
puts output.housenumber
end
の予想される出力
de
Germany
23
マイ出力
[email protected] ~/race (master●)$ ruby read_race.rb [ruby-2.4.0p0]
de
Germany
read_race.rb:49:in `block in <main>': undefined method `housenumber' for #<Country:0x0055e55facf798 @code="de", @name="Germany"> (NoMethodError)
from read_race.rb:46:in `each'
from read_race.rb:46:in `<main>'
おそらく、 'element:housenumber、Integer、:tag =>" housenumber "を' Country'クラスに追加する必要があります。 'housenumber'というメソッドを定義していないので、それから呼び出すことができます。 –
100%確かではない良い例は、あまり明確ではありません。 1つの例では、クラス内で関数を作成しますが、これはhas_many要素用です。 – sayth