Is this old style Ruby?

C

centrepins

In Why's guide, I see the line:

File::eek:pen( ...etc.

Up 'til now I've always written this as:

File.open( ...etc.

(ie. using a . rather than a ::). Am I right in thinking the :: for
accessing class methods is now old-style?

G
 
D

David A. Black

HI --

In Why's guide, I see the line:

File::eek:pen( ...etc.

Up 'til now I've always written this as:

File.open( ...etc.

(ie. using a . rather than a ::). Am I right in thinking the :: for
accessing class methods is now old-style?

I think it still works, though I don't see any advantage to it since
it introduces an unnecessary inconsistency (i.e., something other than
'.' in the position between receiver and message for some subset of
cases).


David
 
J

Jeremy Tregunna

In Why's guide, I see the line:

File::eek:pen( ...etc.

Up 'til now I've always written this as:

File.open( ...etc.

(ie. using a . rather than a ::). Am I right in thinking the :: for
accessing class methods is now old-style?

Foo::bar is for accessing "bar" (be it a method or class or whatever)
in the module "Foo". Foo.bar wants "bar" in class "Foo".
 
N

Nikolai Weibull

* Jeremy Tregunna (Feb 15, 2005 15:10):
Foo::bar is for accessing "bar" (be it a method or class or whatever)
in the module "Foo". Foo.bar wants "bar" in class "Foo".

That's certainly not the whole truth.

Yes, "::" is used for accessing things in modules, but "::" may also be
used for invoking methods of an object or class. It is customary,
however, to use "." for methods of objects and "::" for methods of
classes, although the latter seems to be becoming "." so as to separate
it from its used with modules,
nikoali
 
C

centrepins

Page 349 of the (printed) pickaxe2 mentions '::' and '.', but doesn't
really suggest which one is best to use. So I guess it's possibly one
of those "preffered-style" things?

It seems a little bit odd to me to have two ways of doing the same
thing. Seems best (as was mentioned above) to stick to :: for module
access and . for everything else. ??
 
A

Austin Ziegler

Foo::bar is for accessing "bar" (be it a method or class or whatever)
in the module "Foo". Foo.bar wants "bar" in class "Foo".

Mmm. Not really. I can do this, just fine:

module FooMod
def self.bar; puts "FooMod.bar"; end
end

FooMod.bar
FooMod::bar

The difference between :: and . is that :: is used to access constants
or methods and . is used exclusively to access methods. For fun, try
doing:

"foo"::length

-austin
 
D

David A. Black

Hi --

Page 349 of the (printed) pickaxe2 mentions '::' and '.', but doesn't
really suggest which one is best to use. So I guess it's possibly one
of those "preffered-style" things?

I've never seen :: used where the receiver was anything but a Class or
Module, and usually for constant access rather than method access.
*Please* let's not start seeing it in the general case....
It seems a little bit odd to me to have two ways of doing the same
thing. Seems best (as was mentioned above) to stick to :: for module
access and . for everything else. ??

I would advocate :: for constant access, and . for method access.


David
 
C

centrepins

Actually, should have said "Seems best to stick to :: for module AND
CONSTANT access and . for everything else. "
 
C

centrepins

So you would say use :: only for constant access, and use '.' for
method access be it in a class or module?
 
N

Nikolai Weibull

* (e-mail address removed) (Feb 15, 2005 16:16):
So you would say use :: only for constant access, and use '.' for
method access be it in a class or module?

Well class-methods are constant as well in a sense, hence the use of
"::" in Why's book and it being the (at least old-school) idiom,
nikolai
 
D

David A. Black

Hi --

* (e-mail address removed) (Feb 15, 2005 16:16):

Well class-methods are constant as well in a sense, hence the use of
"::" in Why's book and it being the (at least old-school) idiom,

I disagree; I don't think class methods are constant in any sense, any
more than any other method definition. They're basically just
per-object/singleton method definitions:

class C
def C.x
end
end

like

str = "abc"
def str.x
end

etc. They have a semi-special status in the language (e.g., the
existence of the #class_methods method), but when you call one, you're
just sending a message to an object for method-call-based response.
That's different from retrieving a constant, but it's not different
from calling a method on any other object.


David
 
N

Nikolai Weibull

* David A. Black (Feb 15, 2005 17:10):
I disagree; I don't think class methods are constant in any sense, any
more than any other method definition. They're basically just
per-object/singleton method definitions:

True. Still, there's a subtle difference. I agree, however, that
there's really no good reason to use "::" in this case; I certainly
write

File.open ...

rather than

File::eek:pen ...

which is one symbol longer and doesn't make it easier to read,
nikolai
 
F

Florian Gross

Austin said:
The difference between :: and . is that :: is used to access constants
or methods and . is used exclusively to access methods. For fun, try
doing:

"foo"::length

And you can not use :: for accessing methods that start with an
uppercase letter:

irb(main):001:0> def Xing(); end; self::Xing
TypeError: main is not a class/module
 
R

Robert Klemme

David A. Black said:
Hi --



I've never seen :: used where the receiver was anything but a Class or
Module, and usually for constant access rather than method access.
*Please* let's not start seeing it in the general case....


I would advocate :: for constant access, and . for method access.

+1 (Exactly the convention I follow).

Kind regards

robert
 
J

Jason Voegele

Florian Gross said:
And you can not use :: for accessing methods that start with an
uppercase letter:

That's not exactly true:

irb(main):001:0> Integer("42")
=> 42
irb(main):002:0> Kernel.Integer("42")
=> 42
irb(main):003:0> Kernel::Integer("42")
=> 42

If you supply a parameter list (or even empty parentheses), ruby will use
:: for accessing methods with an uppercase letter, otherwise it will
assume you are trying to access a constant.
irb(main):001:0> def Xing(); end; self::Xing
TypeError: main is not a class/module

irb(main):013:0> self::Xing()
=> nil

--
Jason Voegele
"There is an essential core at the center of each man and woman that
remains unaltered no matter how life's externals may be transformed
or recombined. But it's smaller than we think."
-- Gene Wolfe, The Book of the Long Sun
 

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

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top