[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Noob: use of the double colon

Arie van Wingerden

3/26/2006 12:42:00 PM

Hi,

there seems to be more than one way to execute class methods.
I've seen the form (I use the classmethod new as an example):
newObject = Classname::new
but also
newObject = Classname.new
Bothe seem to work OK.
Is there a difference? If yes, what is the difference?

Also I see :: being used in the context of modules. Can you tell me
about that as well?

Thanks!

Arie

12 Answers

dblack

3/26/2006 1:34:00 PM

0

Jonathan Leighton

3/26/2006 2:25:00 PM

0

On Sun, 2006-03-26 at 22:33 +0900, dblack@wobblini.net wrote:
> Hi --
>
> On Sun, 26 Mar 2006, NuclearFusion wrote:
>
> > Hi,
> >
> > there seems to be more than one way to execute class methods.
> > I've seen the form (I use the classmethod new as an example):
> > newObject = Classname::new
> > but also
> > newObject = Classname.new
> > Bothe seem to work OK.
> > Is there a difference? If yes, what is the difference?
>
> They do both work. I've never understood why the :: exists for this
> purpose, since the . is the normal way of sending messages to objects
> and works perfectly well whether or not the receiver is a class. It's
> on my very short list of things I'd like to see removed from Ruby.
>
> > Also I see :: being used in the context of modules. Can you tell me
> > about that as well?
>
> That's for looking up names of constants in nested scopes:
>
> module M
> X = 1
> end
>
> puts M::X # 1

While we're on the topic -- I've never exactly understood what placing
the double colon at the beginning does? eg...

::FooModule::FOO_CONSTANT

?

Cheers

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



dblack

3/26/2006 2:44:00 PM

0

Jonathan Leighton

3/26/2006 3:31:00 PM

0

On Sun, 2006-03-26 at 23:44 +0900, dblack@wobblini.net wrote:
> Hi --
>
> On Sun, 26 Mar 2006, Jonathan Leighton wrote:
>
> > While we're on the topic -- I've never exactly understood what placing
> > the double colon at the beginning does? eg...
> >
> > ::FooModule::FOO_CONSTANT
> >
> > ?
>
> It forces a top-level lookup. For example:
>
> class M
> class String
> end
>
> def initialize
> @ms = String.new # this will be M::String
> @str = ::String.new("abc") # this will be top-level string
> end
> end
>
> It's like a / at the beginning of a file path.

Ah okay, thanks very much.

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



Arie van Wingerden

3/26/2006 3:33:00 PM

0

Thanx for your clear answers David!
Arie

Mark Volkmann

3/26/2006 3:39:00 PM

0

On 3/26/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 26 Mar 2006, NuclearFusion wrote:
>
> > Hi,
> >
> > there seems to be more than one way to execute class methods.
> > I've seen the form (I use the classmethod new as an example):
> > newObject = Classname::new
> > but also
> > newObject = Classname.new
> > Bothe seem to work OK.
> > Is there a difference? If yes, what is the difference?
>
> They do both work. I've never understood why the :: exists for this
> purpose, since the . is the normal way of sending messages to objects
> and works perfectly well whether or not the receiver is a class. It's
> on my very short list of things I'd like to see removed from Ruby.

Some might say this is another example of TIMTOWTDI.
However, like you, I'd like to see it changed to only use ".".

> > Also I see :: being used in the context of modules. Can you tell me
> > about that as well?
>
> That's for looking up names of constants in nested scopes:
>
> module M
> X = 1
> end
>
> puts M::X # 1

It seems to me that dots could be used for constant references too.
Maybe someone will point out a reason why this would complicate
parsing.

--
R. Mark Volkmann
Object Computing, Inc.


dblack

3/26/2006 3:43:00 PM

0

Ara.T.Howard

3/26/2006 3:54:00 PM

0

dblack

3/26/2006 4:17:00 PM

0

Ara.T.Howard

3/26/2006 4:42:00 PM

0