2

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
kmc
  • 660
  • 13
  • 25
  • What do you mean by "what notation"? It's just a method shortcut. Many of Ruby's "operators" are actually method calls. For example `4 / 2` is equivalent to `4.send(:'/', 2)` – noodl Jan 24 '11 at 13:23
  • @noodl, "For example 4 / 2 is equivalent to 4.send(:'/', 2)". Mostly wrong. The OP wants to know what `/` means in Hpricot, which is an alias to `search`. – the Tin Man Jan 25 '11 at 02:00
  • @the Tin Man: in what universe does "what notation is this" mean "what does this do?" And how is my example wrong? – noodl Jan 25 '11 at 09:54

3 Answers3

4

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
3

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"
mu is too short
  • 426,620
  • 70
  • 833
  • 800
2

/ 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"
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • This has nothing to do with Hpricot. – the Tin Man Jan 25 '11 at 01:46
  • It was meant as answer to "what notation is this", explaining that it is now special notation, just a regular method. If you look at the question title, it says "What is the divisor notation used (for example) in Hpricot?" Since there is no such thing as a "divisor notation", just a `/` method, that was what I answered. – Michael Kohl Jan 25 '11 at 07:39