Forum: Ruby Ruby Forward Slash (/)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Facc5387a195549b6d7a86c3257027bf?d=identicon&s=25 Vincent Predoehl (vpredoehl)
on 2007-06-28 21:46
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?
7e593ac63e5f25649b701dc25f69d1b7?d=identicon&s=25 Luis Parravicini (Guest)
on 2007-06-28 21:52
(Received via mailing list)
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/class...)
 and it's used to make a search.
96931bfe0c2948f47a98e15ae52e5637?d=identicon&s=25 Chris Carter (cdcarter)
on 2007-06-28 21:55
(Received via mailing list)
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-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).
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-06-28 21:56
(Received via mailing list)
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.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-06-28 21:57
(Received via mailing list)
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!)
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-06-28 21:58
(Received via mailing list)
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
Facc5387a195549b6d7a86c3257027bf?d=identicon&s=25 Vincent Predoehl (vpredoehl)
on 2007-06-28 22:22
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.
This topic is locked and can not be replied to.