ruby - 为什么 Ruby Builder 的 XmlMarkup 使用与 groovys 的 NodeBuilder 不同的语法

标签 ruby groovy metaprogramming

Groovys 的 NodeBuilder 使用

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')
  }
}

而 Rails 的 XmlMarkup 使用

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>

为什么在 Rails 中您需要使用显式接收器 (xm) 而在 Groovy 中您可以忽略它?我听说不喜欢 ruby​​ 中的 instance_eval,为什么 groovy 可以摆脱这种风格而 ruby​​ 不能?

谢谢 fractious ,刚刚读完那篇文章,它是对可用于在 ruby​​ 中构建 DSL 的不同技术的精彩总结。我会投票给你,但还没有代表。

最佳答案

这是一篇很好的博文,解释了您可能会采取的不同方式 implementing a DSL in ruby以及每种方法的优缺点。

关于ruby - 为什么 Ruby Builder 的 XmlMarkup 使用与 groovys 的 NodeBuilder 不同的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1421735/

相关文章:

python - 如何通过Python执行任意shell脚本并传递多个变量?

ruby - 如何在 html 输出结果中对 rspec 描述进行分组

grails 2.0.0.RC1 应用程序在 Ubuntu 上失败

python - 转换类名中的字符串(从 appengine 数据存储到类)

macros - 将表达式插入到引号内的表达式中

javascript - Ajax 与 ruby​​ View 相关的问题

ruby - Ruby 中哈希的线程安全

maven - Sonar 未导入coverage.xml结果

json - HTTPBuilder 设置请求内容类型

c# - Functional C# - 使用或返回 Action 的