[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

me

6/1/2006 9:31:00 AM

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.

4 Answers

Dave Burt

6/1/2006 9:54:00 AM

0

me@mikehogan.net 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

me

6/1/2006 4:37:00 PM

0


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.

Dave Burt

6/2/2006 1:03:00 AM

0

me@mikehogan.net 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

joesb.coe9

6/2/2006 3:23:00 AM

0


Dave Burt wrote:
> me@mikehogan.net 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 :@ 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.