acharlieblue
7/3/2005 5:11:00 PM
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"