[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is this old style Ruby?

centrepins

2/15/2005 1:52:00 PM

In Why's guide, I see the line:

File::open( ...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

31 Answers

dblack

2/15/2005 1:59:00 PM

0

Jeremy Tregunna

2/15/2005 2:07:00 PM

0


On 15-Feb-05, at 8:54 AM, centrepins@gmail.com wrote:

> In Why's guide, I see the line:
>
> File::open( ...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".

--
J.



Nikolai Weibull

2/15/2005 2:16:00 PM

0

* 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

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


centrepins

2/15/2005 2:28:00 PM

0

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

Austin Ziegler

2/15/2005 2:28:00 PM

0

On Tue, 15 Feb 2005 23:06:37 +0900, Jeremy Tregunna
<jtregunna@blurgle.ca> wrote:
> On 15-Feb-05, at 8:54 AM, centrepins@gmail.com wrote:
> > In Why's guide, I see the line:
> > File::open( ...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".

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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


dblack

2/15/2005 2:37:00 PM

0

centrepins

2/15/2005 2:43:00 PM

0

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

centrepins

2/15/2005 2:48:00 PM

0

It's all Why?'s fault. Where is he when we want to blame him? Eh?? :o)

Jim Weirich

2/15/2005 2:56:00 PM

0


David A. Black said:
> I would advocate :: for constant access, and . for method access.

This is the style that I personally follow.

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



centrepins

2/15/2005 3:01:00 PM

0

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