Groovys' NodeBuilder uses
def someBuilder = new NodeBuilder()
someBuilder.people(kind:'folks', groovy:true) {
person(x:123, name:'James', cheese:'edam') {
project(name:'groovy')
project(name:'geronimo')
}
person(x:234, name:'bob', cheese:'cheddar') {
project(name:'groovy')
project(name:'drools')
}
}
whereas Rails' XmlMarkup uses
xm = Builder::XmlMarkup.new
xm.instruct! # <?xml version="1.0" encoding="UTF-8"?>
xm.html { # <html>
xm.head { # <head>
xm.title("History") # <title>History</title>
} # </head>
xm.body { # <body>
xm.comment! "HI" # <! -- HI -->
xm.h1("Header") # <h1>Header</h1>
xm.p("paragraph") # <p>paragraph</p>
} # </body>
} # </html>
Why is it that with Rails you need to use an explicit receiver (xm) whereas with Groovy you can ommit it? I've heard about the dislike for instance_eval in ruby, why is it groovy can get away with this style whereas ruby can't?
Thanks fractious, just finished reading that article, it's a superb roundup of the different techniques you can use for building DSLs in ruby. I'd vote you up but no rep yet.