[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

:: and .

joey eisma

11/7/2007 4:45:00 PM

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...
6 Answers

David A. Black

11/7/2007 5:20:00 PM

0

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

--
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.r... for details!

Jason Roelofs

11/7/2007 5:23:00 PM

0

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

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

mortee

11/7/2007 5:29:00 PM

0

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


ara.t.howard

11/7/2007 5:38:00 PM

0


On Nov 7, 2007, at 9:45 AM, 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?
>
> 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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




mortee

11/7/2007 5:55:00 PM

0

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


Alex Young

11/7/2007 6:13:00 PM

0

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

--
Alex