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.
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.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 11.12.2010 14:25, schrieb zuerrong:
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-----
On Sat, 11 Dec 2010 22:25:17 +0900, zuerrong wrote:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs