例如,Hpricot 中使用的除数符号是什么?

发布于 2024-10-14 03:27:42 字数 242 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2024-10-21 03:27:42

Hpricot(和 Nokogiri,因为它支持 Hpricot 的快捷方式)支持两种快捷方式“搜索”(/) 和“at”(%)。

Search 表示“查找此模式的所有出现位置”,at 表示查找第一个出现位置。 Search 返回节点列表,而 at 返回单个节点,当您想要访问节点内容时必须记住这一点。

一般来说,at 适合您知道是唯一且不想迭代的标签或 ID。 搜索 用于遍历表格中的所有行或文档中的每个

标记之类的事情。您还可以从 % 开始链接,这对于查找特定节点然后下降到该节点很有用。

require 'hpricot'

html = '
<html>
  <head><title>blah</title>
  <body>
    <div id="foo">
      <p>paragraph1</p>
      <p>paragraph2</p>
    </div>
  </body>
</head>
'
doc = Hpricot(html)

doc.at('title').inner_text  # => "blah"
(doc / 'p').last.inner_text # => "paragraph2"
(doc % 'p').inner_text # => "paragraph1"
(doc % '#foo').search('p').size # => 2

就我个人而言,我推荐 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" and at means find the first occurrence. Search returns a list of nodes, while at 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.

require 'hpricot'

html = '
<html>
  <head><title>blah</title>
  <body>
    <div id="foo">
      <p>paragraph1</p>
      <p>paragraph2</p>
    </div>
  </body>
</head>
'
doc = Hpricot(html)

doc.at('title').inner_text  # => "blah"
(doc / 'p').last.inner_text # => "paragraph2"
(doc % 'p').inner_text # => "paragraph1"
(doc % '#foo').search('p').size # => 2

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.

梦回梦里 2024-10-21 03:27:42

该表示法可能旨在使用重载的 / 运算符来调用 XPath :

/ 选择文档根(始终是文档元素的父元素)

该运算符需要两个参数,并且 LHS 提供重载上下文,因此您必须说

doc/"p.posted"

而不仅仅是

/"p.posted"

The notation is probably meant to evoke XPath using an overloaded / operator:

/ selects the document root (which is always the parent of the document element)

The operator needs two arguments and the LHS supplies the overloading context so you have to say

doc/"p.posted"

rather than just

/"p.posted"
童话里做英雄 2024-10-21 03:27:42

/ 只是一种常规方法,可以以中缀样式调用:

>> 8 / 2 #=> 4
>> 8./ 2 #=> 4

只需为您自己的类定义一个:

>> class Myclass
..   def /(n)
..     "Yeah" * n
..     end
..   end #=> nil
>> Myclass.new / 5 #=> "YeahYeahYeahYeahYeah"

/ is just a regular method, which can be called in an infix style:

>> 8 / 2 #=> 4
>> 8./ 2 #=> 4

Just define one for your own classes:

>> class Myclass
..   def /(n)
..     "Yeah" * n
..     end
..   end #=> nil
>> Myclass.new / 5 #=> "YeahYeahYeahYeahYeah"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文