Nokogiri - 如何命名节点“评论”?

Nokogiri - How to name a node `comment`?

我正在使用 Nokogiri 创建一些 XML:

def builder
  Nokogiri::XML::Builder.new do |xml|
    xml.foobar do
      xml.comment('Some comment', created_at: Time.zone.now.iso8601)
    end
  end
end

我想要这个结构:

<foobar>
  <comment created_at='...'>
   Some comment
  </comment>
</foobar>

不幸的是,Nokogiri DSL 不允许我命名节点 comment,因为它是创建 XML-评论的内部方法。我怎样才能做到这一点?

来自文档:http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Builder

Unfortunately some methods are defined in ruby that are difficult or dangerous to remove. You may want to create tags with the name “type”, “class”, and “id” for example. In that case, you can use an underscore to disambiguate your tag name from the method call.

因此你需要:

def builder
  Nokogiri::XML::Builder.new do |xml|
    xml.foobar do
      xml.comment_('Some comment', created_at: Time.zone.now.iso8601)
    end
  end
end