Is this old style Ruby?

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

HI --

···

On Tue, 15 Feb 2005 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?

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

--
David A. Black
dblack@wobblini.net

* 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);}

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

···

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

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Hi,

···

Am Dienstag, 15. Feb 2005, 22:54:54 +0900 schrieb centrepins@gmail.com:

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?

`::' may be used to qualify top-level constants:

  X=1 ; class C ; X=2 ; def C.f ; ::X ; end ; end ; C.f
  # => 1

You can't do that with `.'.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

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 wrote:

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

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

···

On Tue, 15 Feb 2005 centrepins@gmail.com wrote:

--
David A. Black
dblack@wobblini.net

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

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

In Message-Id: <1108477682.487306.150420@c13g2000cwb.googlegroups.com>
centrepins@gmail.com writes:

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

For consistency use ".", for uniformity with other languages use
"::". Historically Foo::foo() notation was introduced for those who
said that could be more understandable for C++ or Java people.

For my own preference, I use "." since that consistent in Ruby ---
in Foo.foo, receiver is Foo itself --- and "::" notation always needs
() if method name is capitalized.

# Well, capitalized name of class methods also out of my preference....

    > ruby
    class Foo; def self.foo; p :foo; end; end
    Foo.foo
    Foo::foo
    :foo
    :foo
    > ruby
    class Foo; def self.Bar; p :bar; end; end
    Foo.Bar
    Foo::Bar
    :bar
    -:3: uninitialized constant Foo::Bar (NameError)

···

--
kjana@dm4lab.to February 16, 2005
Better late than never.

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

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://onestepback.org
-----------------------------------------------------------------
"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)

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0502150635500.30704@wobblini...

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.

+1 (Exactly the convention I follow).

Kind regards

    robert

···

On Tue, 15 Feb 2005 centrepins@gmail.com wrote:

That's exactly why I use . for both class and instance method-calls
and :: for constants, there still is this overlap but a better
semantical devision. Since :: gives a feel for working with
"namespaces" (Foo::Bar, Foo::Bar::Konstant) and class and module names are
constants too. The . is well-known for method calls (regardless the tape)
and you also don't do: def MyClass::classmethod; ... ; end.

That's the guideline I follow at least, just my 2¢.
It seems to be clearer for RubyNubies too (in my experience explaining
instance methods, class methods and namespaces to them).

Paul

···

On Wed, Feb 16, 2005 at 06:13:28AM +0900, Jason Voegele wrote:

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

That's not exactly true:

[...]

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.

--
Student @ Eindhoven | email: paul@luon.net
University of Technology, The Netherlands | JID: paul@luon.net

Using the Power of Debian GNU/Linux <<< | GnuPG key ID: 0x50064181

David A. Black wrote:

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

Some people do it -- I've seen it before, but I don't advocate it.
It smells like C++ to me.

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.

I also advocate . for methods.

:: is necessary for constants. (I once started a thread where I wondered
why we couldn't use dot for constants -- but eventually I painted myself
into a corner and realized "it's already done the right way.")

Hal

···

On Tue, 15 Feb 2005 centrepins@gmail.com wrote:

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

Robert Klemme wrote:

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0502150635500.30704@wobblini...

--- 8< ---[snip]---

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

+1 (Exactly the convention I follow).

+= 1

Happy rubying

Stephan

* centrepins@gmail.com (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

···

--
::: 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);}

Hi --

···

On Wed, 16 Feb 2005 centrepins@gmail.com wrote:

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

That makes sense to me.

David

--
David A. Black
dblack@wobblini.net