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

M

me

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.
 
D

Dave Burt

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
 
M

me

Dave said:
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.
 
D

Dave Burt

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
 
J

joesb

Dave said:
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
=> # said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top