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!
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
···
On 11/7/07, joey eisma <joey_eisma@yahoo.com> 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?
thanks!
joey
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
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.
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
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