different between the "::" and "."

Z

zuerrong

Hello,

class Myclass
def Myclass.hi
"hi"
end
def Myclass::hello
"hello"
end
end

what's the difference between Myclass.hi and Myclass::hello here?

Thanks.
 
Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 11.12.2010 14:25, schrieb zuerrong:
Hello,

class Myclass
def Myclass.hi
"hi"
end
def Myclass::hello
"hello"
end
end

what's the difference between Myclass.hi and Myclass::hello here?

Thanks.

In your example they're just the same. But have a look at this:

- --------------------------------------
module Foo

class Bar
end

end

x = Foo::Bar.new #=> a new instance of Foo::Bar
x = Foo.Bar.new #=> error
- --------------------------------------

The :: operator is normally used to resolve namespaces--in your example
you used it for a class method which is perfectly fine although I think
this style is old-ish and not used widely anymore. I personally use the
point operator for calling methods on any objects. Classes are just
normal objects in Ruby, so why use a special syntax there?

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJNA3U1AAoJEGrS0YjAWTKVka0H/0xbCOqH5Gbugleh8b9BpGNX
CE6vGbHHuwXaf0rdZTJTL95660hVN1ucFbL2gMH8WKl3A1VqaQQlLdY6EG3sBYEc
7aBlGT54XP9t2B/zRMyHqxgVegbs6HwYyotGYx24FjMtgj/JtXUu0Vq1P2Lf1Jz1
thU0aics3pAbN68xcJdoToe1WCw1KHdvbC4ywZq8fhP3uLyWRqA92l2q28MCz59O
P3SkrDoD3yEZBvruCoui7L+SPkqDscdZAt8LrpWFxnZZHxMiMeKL34GdKRdZMYUQ
fh4JDuvOeSTRVZKqpgttdxbcwq3e1G1EYVdogIwECSONj2Bh/FiIUYPxPCysjXA=
=tKRW
-----END PGP SIGNATURE-----
 
K

Ken Bloom

Hello,

class Myclass
def Myclass.hi
"hi"
end
def Myclass::hello
"hello"
end
end

what's the difference between Myclass.hi and Myclass::hello here?

Thanks.

Quoting Pickaxe (p349):

The only difference between :: and . occurs with uppercase identifiers.
Ruby will assume that receiver::Thing is actually an attempt to access a
constant named Thing unless the method invocation has a parameter list
between parentheses
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top