tags:

views:

44

answers:

2
module Hints
    module Designer
        def self.message
            "Hello, World!"
        end
    end
end

is there any way to use the following code to access the message method?

p Hints.Designer.message

Instead of

p Hints::Designer.message
+1  A: 

You can use dot to access the message method, but you can't use it to access Designer module, because Designer it is not a method (but a constant). Dot is for invoking methods only.

Mladen Jablanović
just as we have namespaces in dotnet... they are accessed by dot notation... so what i want is to access them with the dot notation same as dotnet... can i..? any any any way..?
Jamal Abdul Nasir
Not sure if I understand... You want to use C# syntax in Ruby? No can do.
Mladen Jablanović
ok... thnx aloadz brother...
Jamal Abdul Nasir
+4  A: 

The period . is only meant for accessing methods.

The double colon :: is used to indicate namespaces.

Both modules and classes can be nested in each other. This creates a namespace for the nested class. (Technically, Module is an instance of Class.) Thus, the following is correct no matter if Hints or Designer are a class or a module.

Hints::Designer.message

You can try yourself by opening irb on the command line. Hints.Designer.message says NoMethodError: undefined method 'Designer' for Hints:Module.

Update (as I am not allowed to comment...):

While many things in Ruby can be overwritten ("monkey patched"), basic operators cannot. :: is a basic language feature that is and should not be customizable (in order to prevent a big mess ;)).

duddle