Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:13:51+00:00 2026-05-29T23:13:51+00:00

Documentation I’ve read tells me to use Module.method to access methods in a module.

  • 0

Documentation I’ve read tells me to use Module.method to access methods in a module. However, I can use Module::method as well. Is this syntactic sugar, or am I confused?

module Cat
    FURRY_LEVEL = 4

    def self.sound
        %w{meow purr hiss zzzz}.sample
    end
end

puts Cat.sound          # This works.
puts Cat::sound         # This also works.  Why?!

puts Cat.FURRY_LEVEL    # Expected error occurs here.
puts Cat::FURRY_LEVEL   # This works.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:13:54+00:00Added an answer on May 29, 2026 at 11:13 pm

    Constant resolution always requires that you use ::.

    Method invocation is idiomatically and usually a period (.), but :: is also legal. This is not just true for so-called module methods, but for invoking any method on any object:

    class Foo
      def bar
        puts "hi"
      end
    end
    
    Foo.new::bar
    #=> hi
    

    It’s not so much “syntax sugar” as it is simply alternative syntax, such as the ability to write if or case statements with either a newline, then and newline, or just then.

    It is specifically allowed because Ruby allows methods with the same name as a constant, and sometimes it makes sense to think that they are the same item:

    class Foo
      class Bar
        attr_accessor :x
        def initialize( x )
          self.x = x
        end
      end
      def self.Bar( size )
        Foo::Bar.new( size ) 
      end
    end
    
    p Foo::Bar     #=> Foo::Bar                    (the class)
    p Foo::Bar(42) #=> #<Foo::Bar:0x2d54fc0 @x=42> (instance result from method call)
    

    You see this commonly in Ruby in the Nokogiri library, which has (for example) the Nokogiri::XML module as well as the Nokogiri.XML method. When creating an XML document, many people choose to write

    @doc = Nokogiri::XML( my_xml )
    

    You see this also in the Sequel library, where you can write either:

    class User < Sequel::Model                      # Simple class inheritance
    class User < Sequel::Model(DB[:regular_users])  # Set which table to use
    

    Again, we have a method (Sequel.Model) named the same as a constant (Sequel::Model). The second line could also be written as

    class User < Sequel.Model(DB[:regular_users])
    

    …but it doesn’t look quite as nice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The documentation we can use auto_import if we need access to the data but
The documentation for Access.BackingField() indicates that this: Sets the access-strategy to use the backing-field
The documentation says that... You can use the ToLocalTime method to restore a local
MSDN documentation for classes contain many inherited properties and methods. I think this is
Documentation can be found here It says in the example: onDrop: Called whenever a
The documentation with the module itself is pretty thin, and just tends to point
The documentation for +[NSThread detachNewThreadSelector:toTarget:withObject:] says: For non garbage-collected applications, the method aSelector is
Documentation states that interface delegation is available for Win32 only. Currently I can't test
The documentation for NSDictionary says that the method objectForKey returns nil if the NSDictionary
The documentation says there's a limit to the number of regions that can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.