Two questions: one for XML Builder, one for ERB

1.
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>

The code is clear, but how to understand "builder.person"? A dynamic
method?

2.

require 'erb'
File.open( ARGV[0] ) { |fh|
erb = ERB.new( fh.read )
print erb.result( binding )

}

How to understand "binding"? Where does it defined?

Thanks for any help!

Greg Chen(China) wrote:

1.
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>

The code is clear, but how to understand "builder.person"? A dynamic
method?

Yep. Builder uses method_missing to dynamically create markup methods
as they are called.

2.

require 'erb'
File.open( ARGV[0] ) { |fh|
erb = ERB.new( fh.read )
print erb.result( binding )

}

How to understand "binding"? Where does it defined?

It's defined in the Kernel module. From "ri Kernel.binding":

···

---------------------------------------------------------
Kernel#binding
     binding -> a_binding
------------------------------------------------------------------------
     Returns a +Binding+ object, describing the variable and method
     bindings at the point of call. This object can be used when
calling
     +eval+ to execute the evaluated command in this environment. Also
     see the description of class +Binding+.

        def getBinding(param)
          return binding
        end
        b = getBinding("hello")
        eval("param", b) #=> "hello"

The following articles talk about builder ...

http://www.onestepback.org/index.cgi/Tech/Ruby/BuilderObjects.rdoc
http://www.onestepback.org/index.cgi/Tech/Ruby/StayingSimple.rdoc

···

On Sunday 03 July 2005 09:35 am, Greg Chen(China) wrote:

The code is clear, but how to understand "builder.person"? A dynamic
method?

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)