How do you know what methods do not require a dot prefix

Discussion in 'Ruby' started by me@mikehogan.net, Jun 1, 2006.

  1. Guest

    The Poignant Guide to Ruby gives this example:

    email = if at_hotel
    address = "why"
    address << "@hotelambrose"
    address << ".com"
    end

    which equates to

    address.<<( ".com" )

    However, "com".length cannot be written as "com" length.

    How can I tell what methods require the dot prefix, and which methods
    do not?

    Thanks very much,
    Mike.
     
    , Jun 1, 2006
    #1
    1. Advertising

  2. Dave Burt Guest

    wrote:
    > The Poignant Guide to Ruby gives this example:
    >
    > email = if at_hotel
    > address = "why"
    > address << "@hotelambrose"
    > address << ".com"
    > end
    >
    > which equates to
    >
    > address.<<( ".com" )
    >
    > However, "com".length cannot be written as "com" length.
    >
    > How can I tell what methods require the dot prefix, and which methods
    > do not?


    Generally, all methods need the dot.

    "foo << bar" is not method call syntax, << is a special operator.

    There are certain other operators that call methods behind-the-scenes: +
    - * ** / % =~ == === <=> < <= > >= & | [] []= @+ @- @~. But you can see
    the pattern - they're all "operators" - symbols not words. Some of them
    aren't even "foo + bar" style operators... there's "foo[bar]" and
    "foo[bar]=baz" and "+foo".

    Note, though, that you can call methods on self without a dot, or
    "self". A shortcut for 'self.puts "hello world"' is:

    puts "hello world"

    Cheers,
    Dave
     
    Dave Burt, Jun 1, 2006
    #2
    1. Advertising

  3. Guest

    Dave Burt wrote:

    > There are certain other operators that call methods behind-the-scenes: +
    > - * ** / % =~ == === <=> < <= > >= & | [] []= @+ @- @~. But you can see
    > the pattern - they're all "operators" - symbols not words.


    So this list of operators, am i right in assuming that its defined in
    the ruby interpreter in C code, and is not something that I can add to?

    Thanks,
    Mike.
     
    , Jun 1, 2006
    #3
  4. Dave Burt Guest

    wrote:
    >> There are certain other operators that call methods behind-the-scenes: +
    >> - * ** / % =~ == === <=> < <= > >= & | [] []= @+ @- @~. But you can see
    >> the pattern - they're all "operators" - symbols not words.

    >
    > So this list of operators, am i right in assuming that its defined in
    > the ruby interpreter in C code, and is not something that I can add to?


    That's right. It's part of Ruby's syntax.

    Cheers,
    Dave
     
    Dave Burt, Jun 2, 2006
    #4
  5. joesb Guest

    Dave Burt wrote:
    > wrote:
    > >> There are certain other operators that call methods behind-the-scenes: +
    > >> - * ** / % =~ == === <=> < <= > >= & | [] []= @+ @- @~. But you can see
    > >> the pattern - they're all "operators" - symbols not words.

    > >
    > > So this list of operators, am i right in assuming that its defined in
    > > the ruby interpreter in C code, and is not something that I can add to?

    >
    > That's right. It's part of Ruby's syntax.
    >
    > Cheers,
    > Dave


    It would be cool, if not chaos, if Ruby allowed one to define binary
    operator (and also may be ternary, too?) like in Haskell.

    Haskell allow one to create binary operator simply by naming a function
    using only symbol.
    So function named 'foo' is call as
    (foo 1 2)
    but function '+', '-->', '<~>' are call as
    1 --> 2
    You can also use 'foo' as operator by putting backquote around it as
    1 `foo` 2
    or call '-->' as simple function with
    ((-->) 1 2).

    So Ruby with this extension would look like.

    def Number
    # create a point
    def @(y)
    Point.new(self, y)
    end
    end

    > 1 @ 2

    => #<Point :x=>1, :y=> 2>
    > 1.'@'(2) # call using normal 'dot' way.

    => #<Point :x=>1, :y=> 2>

    The use of single quote 1.'@'(2) looks consistent with how symbol are
    quoted in Ruby -- you can't write :mad: but you have to write :'@' to
    specify symbol '@'.


    This should illiminate the different between operator and method.
    It's a little syntactic sugar, but allow better DSL and consistency.
     
    joesb, Jun 2, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Tyler
    Replies:
    36
    Views:
    1,473
    Darryl L. Pierce
    Dec 10, 2004
  2. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    432
    Samuël van Laere
    Oct 16, 2003
  3. Dave
    Replies:
    10
    Views:
    625
    Jonathan N. Little
    Sep 22, 2006
  4. Chris  Chiasson
    Replies:
    6
    Views:
    625
    Richard Tobin
    Nov 14, 2006
  5. Replies:
    6
    Views:
    253
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page