Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do you know what methods do not require a dot prefix

Reply
Thread Tools

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

 
 
me@mikehogan.net
Guest
Posts: n/a
 
      06-01-2006
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.

 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      06-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
me@mikehogan.net
Guest
Posts: n/a
 
      06-01-2006

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.

 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      06-02-2006
(E-Mail Removed) 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
 
Reply With Quote
 
joesb
Guest
Posts: n/a
 
      06-02-2006

Dave Burt wrote:
> (E-Mail Removed) 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 =>1, :y=> 2>
> 1.'@'(2) # call using normal 'dot' way.

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

The use of single quote 1.'@'(2) looks consistent with how symbol are
quoted in Ruby -- you can't write :@ 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
removing a namespace prefix and removing all attributes not in that same prefix Chris Chiasson XML 6 11-14-2006 05:08 PM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
"static" prefix - to parallel "this" prefix Tim Tyler Java 36 12-10-2004 01:02 AM
To dot or not to dot? SamuŽl van Laere HTML 8 10-16-2003 02:55 PM



Advertisments