Different between the "::" and "."

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

···

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.

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

···

On Sat, 11 Dec 2010 22:25:17 +0900, zuerrong wrote:

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.

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/