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.
Vincent P. (Guest)
on 2007-06-28 23: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?
Luis P. (Guest)
on 2007-06-28 23:52
(Received via mailing list)
On 6/28/07, Vincent P. <removed_email_address@domain.invalid> 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.
Chris C. (Guest)
on 2007-06-28 23:55
(Received via mailing list)
On 6/28/07, Vincent P. <removed_email_address@domain.invalid> 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).
Trans (Guest)
on 2007-06-28 23:56
(Received via mailing list)
On Jun 28, 3:46 pm, Vincent P. <removed_email_address@domain.invalid>
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.
Gavin K. (Guest)
on 2007-06-28 23:57
(Received via mailing list)
On Jun 28, 1:46 pm, Vincent P. <removed_email_address@domain.invalid>
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. (Guest)
on 2007-06-28 23:58
(Received via mailing list)
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
Vincent P. (Guest)
on 2007-06-29 00:22
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.
This topic is locked and can not be replied to.