[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Forward Slash (/

Vincent Predoehl

6/28/2007 7:47:00 PM

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

6 Answers

Gavin Kistner

6/28/2007 7:52:00 PM

0

On Jun 28, 1:46 pm, Vincent Predoehl <vpredo...@phoenixwebgroup.com>
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!)

Luis Parravicini

6/28/2007 7:52:00 PM

0

On 6/28/07, Vincent Predoehl <vpredoehl@phoenixwebgroup.com> 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.ht...)
and it's used to make a search.

--
Luis Parravicini
http://ktulu.co...

Chris Carter

6/28/2007 7:53:00 PM

0

On 6/28/07, Vincent Predoehl <vpredoehl@phoenixwebgroup.com> 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-....
>
>

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


--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Trans

6/28/2007 7:55:00 PM

0



On Jun 28, 3:46 pm, Vincent Predoehl <vpredo...@phoenixwebgroup.com>
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.


Bertram Scharpf

6/28/2007 7:57:00 PM

0

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Vincent Predoehl

6/28/2007 8:23:00 PM

0

Bertram Scharpf wrote:
> 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


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

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