by scott_s · 16y ago · 0 comments
require 'rubygems'
require_gem 'builder'
builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
builder.person { |b| b.name("Jim"); b.phone("555-1234") }
# Prints:
# <person>
# <name>Jim</name>
# <phone>555-1234</phone>
# </person>
Also see this recent HN thread: http://news.ycombinator.com/item?id=1626018Instead of:
builder.person do |b|
b.name("Jim")
b.phone("555-1234")
end
You could write it like this: builder.tag!('person') do |b|
b.tag!('name', "Jim")
b.tag!('phone', "555-1234")
endI think the ActiveRecord example below is the answer I'm looking for, since it creates a method at runtime based on information that did not exist until runtime. But I need to read up on it to understand it.
builder.person do |b|
b.name = "Jim"
b.phone = "555-1234"
end