Skip to content

Instantly share code, notes, and snippets.

@samsouder
Created March 11, 2009 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsouder/77620 to your computer and use it in GitHub Desktop.
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.
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>
@eskignax
Copy link

Same problem here.
Did you find why ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment