[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

two questions: one for XML Builder, one for ERB.

Greg Chen(China)

7/3/2005 1:32:00 PM

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!

2 Answers

acharlieblue

7/3/2005 5:11:00 PM

0


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"

Jim Weirich

7/3/2005 9:37:00 PM

0

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?

The following articles talk about builder ...

http://www.onestepback.org/index.cgi/Tech/Ruby/BuilderOb...
http://www.onestepback.org/index.cgi/Tech/Ruby/StayingS...

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"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)