Ruby Forward Slash (/)

V

Vincent Predoehl

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?
 
P

Phrogz

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!)
 
C

Chris Carter

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?

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).
 
T

Trans

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.
 
B

Bertram Scharpf

Hi,

Am Freitag, 29. Jun 2007, 04:46:53 +0900 schrieb Vincent Predoehl:
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
 
V

Vincent Predoehl

Bertram said:
Hi,

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top