:: and

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

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

Hi –

On Thu, 8 Nov 2007, joey eisma wrote:

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

On Nov 7, 2007, at 9:45 AM, joey eisma wrote:

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::object_id ’
76540

regards.

a @ http://codeforpeople.com/

joey eisma wrote:

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

joey eisma wrote:

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

mortee wrote:

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