Created
March 11, 2009 18:10
-
-
Save samsouder/77620 to your computer and use it in GitHub Desktop.
For some reason, I can't access a class variable inside a Nokogiri block like I can inside Builder blocks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'builder' # 2.1.2 | |
require 'nokogiri' # 1.2.1 | |
@testing = { :item1 => 'value1', :item2 => 'value2', :item3 => 'value3' } | |
def nokogiri | |
Nokogiri::XML::Builder.new { | |
root { | |
items { | |
if @testing | |
@testing.each_pair do |key, value| | |
item { text value } | |
end | |
end | |
} | |
} | |
} | |
end | |
def builder | |
xml = Builder::XmlMarkup.new(:indent => 1) | |
xml.root { | |
xml.items { | |
if @testing | |
@testing.each_pair do |key, value| | |
xml.item value | |
end | |
end | |
} | |
} | |
xml | |
end | |
puts 'Builder:' | |
puts builder.target! | |
puts 'Nokogiri:' | |
puts nokogiri.to_xml | |
# OUTPUT | |
# Builder: | |
# <root> | |
# <items> | |
# <item>value1</item> | |
# <item>value2</item> | |
# <item>value3</item> | |
# </items> | |
# </root> | |
# Nokogiri: | |
# <?xml version="1.0"?> | |
# <root> | |
# <items/> | |
# </root> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same problem here.
Did you find why ?