例如,Hpricot 中使用的除数符号是什么?
在 Hpricot 文档(位于 https://github.com/hpricot/hpricot)中有一个文档。搜索()方法。然后文档继续说“快捷方式是使用除数”:
(doc/"p.posted")
它可以工作,这是肯定的,但我想知道,这是什么表示法?我以前从未遇到过。
In the Hpricot docs (at https://github.com/hpricot/hpricot) there is a doc.search() method. The docs then go on to say "A shortcut is to use the divisor":
(doc/"p.posted")
It works, that's for sure, but I'm wondering, what notation is this? I have never come across it before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Hpricot(和 Nokogiri,因为它支持 Hpricot 的快捷方式)支持两种快捷方式“搜索”(
/
) 和“at”(%
)。Search
表示“查找此模式的所有出现位置”,at
表示查找第一个出现位置。Search
返回节点列表,而at
返回单个节点,当您想要访问节点内容时必须记住这一点。一般来说,
at
适合您知道是唯一且不想迭代的标签或 ID。搜索
用于遍历表格中的所有行或文档中的每个标记之类的事情。您还可以从
%
开始链接,这对于查找特定节点然后下降到该节点很有用。就我个人而言,我推荐 Nokogiri 而不是 Hpricot。它支持所有快捷方式,但功能更全面,并且支持得很好。
而且,快捷方式
/
和%
不是我见过的任何标准的一部分;它们是赫普里科特 (Hpricot) 本地的,为了方便起见,诺科吉里 (Nokogiri) 继承了它们。我不记得在 Perl 或 Python 解析器中见过它们。Hpricot (and Nokogiri, because it support's Hpricot's shortcuts) supports two shortcut methods for "search", (
/
) and "at" (%
).Search
means "find all occurrences of this pattern" andat
means find the first occurrence.Search
returns a list of nodes, whileat
returns a single node, which you have to keep in mind when you want to access the contents of the node.Generally,
at
is good for tags or IDs you know to be unique and will not want to iterate over.Search
is for things like walking over all rows in a table, or every<p>
tag in a document. You can also chain from an%
, which is useful for finding a particular node, then descending into it.Personally, I recommend Nokogiri over Hpricot. It supports all the short-cuts but is more full-featured, and very well supported.
And, the shortcuts
/
and%
are not parts of any standard that I've seen; They're local to Hpricot, and were inherited by Nokogiri for convenience. I don't remember seeing them in Perl or Python parsers.该表示法可能旨在使用重载的
/
运算符来调用 XPath :该运算符需要两个参数,并且 LHS 提供重载上下文,因此您必须说
而不仅仅是
The notation is probably meant to evoke XPath using an overloaded
/
operator:The operator needs two arguments and the LHS supplies the overloading context so you have to say
rather than just
/
只是一种常规方法,可以以中缀样式调用:只需为您自己的类定义一个:
/
is just a regular method, which can be called in an infix style:Just define one for your own classes: