:: and .

J

joey eisma

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

hi!

reading through some tutorial, i noticed the author would interchange using :: and . to call methods. i see they both give the same result.

i was wondering if there is any other difference between the two? (other than convenience maybe?). when would you use one over the other?

thanks!

joey

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
D

David A. Black

Hi --

hi!

reading through some tutorial, i noticed the author would interchange using :: and . to call methods. i see they both give the same result.

i was wondering if there is any other difference between the two? (other than convenience maybe?). when would you use one over the other?

I never use :: to call methods. Some people use it to call class
methods, but I've never seen any reason to switch operators just
because the receiver is a class or module.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
J

Jason Roelofs

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

They don't both always give the same result. "." is the message operator and
"::" is the scoping operator. In the case of methods, they both work the
same, but in the case of constants, you will get an "undefined method
"CONSTANT"" if you use ".".

So explicit scoping / getting nested constants, you have to use ::

For class methods, you can use either. I prefer "."

Jason
 
M

mortee

joey said:
hi!

reading through some tutorial, i noticed the author would interchange
using :: and . to call methods. i see they both give the same result.


i was wondering if there is any other difference between the two?
(other than convenience maybe?). when would you use one over the
other?

It's described somewhere in the pickaxe book. IIRC when you access a
member of a module using ::, without trailing parentheses, then it's
considered accessing a constant. All other cases are method calls.

mortee
 
A

ara.t.howard

hi!

reading through some tutorial, i noticed the author would
interchange using :: and . to call methods. i see they both give
the same result.

i was wondering if there is any other difference between the two?
(other than convenience maybe?). when would you use one over the
other?

thanks!

i occasionally use '::' to get class values that are effectively
constants/config. for instance

if Monitor::interval > 42

also, i tend to use '::' if the class in question is stored in a
variable, for example

c = method_that_returns_a_class

...

value = c::some_method

simply because, when the code becomes complex, it helps my feeble
mind recall that 'c' is indeed a class, and not a basic object.

mostly though i think this is a religious issue. fyi, you can
interchange '.' and '::' for normal objects too

cfp:~ > ruby -e' p Object::new::eek:bject_id '
76540

regards.

a @ http://codeforpeople.com/
 
M

mortee

mortee said:
It's described somewhere in the pickaxe book. IIRC when you access a
member of a module using ::, without trailing parentheses, then it's
considered accessing a constant. All other cases are method calls.

Ah yes, after seeing the other posts in this thread, I have to add that
it only applies if the name after the :: starts with a capital letter.
Of course you can't have constants starting with lowercase letters, so

MyModule::some_member

is also a method call.

mortee
 
A

Alex Young

joey said:
hi!

reading through some tutorial, i noticed the author would interchange using :: and . to call methods. i see they both give the same result.

i was wondering if there is any other difference between the two? (other than convenience maybe?). when would you use one over the other?
In addition to the other answers, there's one place that you can only
use '::' rather than '.', and that's when you're specifying that your
constant should be searched for in the root namespace:

Bar = 42
module Foo
Bar = 23
def Foo.show
p ::Bar
p Bar
end
end

Foo.show outputs:
42
23
 

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

Similar Threads

specifying path 1
Magazine name 1
Simple Q: bit string -> integer 2
What is the meaning of Dim 6
how to run SciTE from any path 1
daemons 0
where does song.name come from? 3
Leaving Python List 1

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top