Ruby Forward Slash (/)

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/"#sidebar").remove

What does the slash do, is it an operator? What’s it called?

On 6/28/07, Vincent P. [email protected] wrote:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/“#sidebar”).remove

What does the slash do, is it an operator? What’s it called?

It’s from hpricot
(http://code.whytheluckystiff.net/doc/hpricot/classes/Hpricot/Traverse.html#M000055)
and it’s used to make a search.

On Jun 28, 3:46 pm, Vincent P. [email protected]
wrote:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/“#sidebar”).remove

What does the slash do, is it an operator? What’s it called?

That’s not an official part of Ruby. It looks like an Hpricot method.
Hpricot is used to scan xml/html docs and is based on jQeury. However,
since _why introduced this notation I’ve used it myself where it
seemed suitable. For instance Facets extends Hash with #/:

h = {:a => 1}
h/:a #=> 1

T.

On 6/28/07, Vincent P. [email protected] wrote:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/“#sidebar”).remove

What does the slash do, is it an operator? What’s it called?


Posted via http://www.ruby-forum.com/.

The / is normally the division operator, but like most operators in
ruby, it can be overridden. In this case, it is using the hpricot
library, and / is an alias to #select (or #at, can’t remember which is
which).

On Jun 28, 1:46 pm, Vincent P. [email protected]
wrote:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/“#sidebar”).remove

What does the slash do, is it an operator? What’s it called?

It’s a method call, as with most other ‘operators’.

irb(main):001:0> 10 / 2
=> 5
irb(main):002:0> class Fixnum; def /(*a); puts “Ha ha!”; end; end
=> nil
irb(main):003:0> 10 / 2
Ha ha!
=> nil

(Note how it doesn’t even return the right value anymore!)

Bertram S. wrote:

Hi,

Am Freitag, 29. Jun 2007, 04:46:53 +0900 schrieb Vincent P.:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/"#sidebar").remove

What does the slash do, is it an operator? What’s it called?

What does

doc.class

say?

Maybe this code make it a little more clear:

class S ; def / oth ; puts oth.inspect ; self ; end ; end
s = S.new
s.resond_to? :"/"
s / “dummy”

Bertram

ok, it looks like they just overode the operator /. I thought it was a
standard part of the Ruby language.

Hi,

Am Freitag, 29. Jun 2007, 04:46:53 +0900 schrieb Vincent P.:

I thought I knew everything about the Ruby syntax, then I saw this:

(doc/"#sidebar").remove

What does the slash do, is it an operator? What’s it called?

What does

doc.class

say?

Maybe this code make it a little more clear:

class S ; def / oth ; puts oth.inspect ; self ; end ; end
s = S.new
s.resond_to? :"/"
s / “dummy”

Bertram