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

  • SEARCH
  • Home
  • 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 897947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:57:51+00:00 2026-05-15T14:57:51+00:00

According to the documentation mod.const_get(sym) Returns the value of the named constant in mod.

  • 0

According to the documentation mod.const_get(sym) “Returns the value of the named constant in mod.”

I also know that const_get by default may look up the inheritance chain of the receiver. So the following works:

class A; HELLO = :hello; end
class B < A; end
B.const_get(:HELLO) #=> :hello

I also know that classes in Ruby subclass Object, so that you can use const_get to look up ‘global’ constants even though the receiver is a normal class:

class C; end
C.const_get(:Array) #=> Array

However, and this is where i’m confused — modules do not subclass Object. So why can I still look up ‘global’ constants from a module using const_get? Why does the following work?

module M; end
M.const_get(:Array) #=> Array

If the documentation is correct – const_get simply looks up the constant defined under the receiver or its superclasses. But in the code immediately above, Object is not a superclass of M, so why is it possible to look up Array ?

Thanks

  • 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-15T14:57:52+00:00Added an answer on May 15, 2026 at 2:57 pm

    You are correct to be confused… The doc didn’t state that Ruby makes a special case for lookup of constants in Modules and has been modified to state this explicitly. If the constant has not been found in the normal hierarchy, Ruby restarts the lookup from Object, as can be found in the source.

    Constant lookup by itself can be bit confusing. Take the following example:

    module M
      Foo = :bar
      module N
        # Accessing Foo here is fine:
        p Foo # => bar
      end
    end
    module M::N
      # Accessing Foo here isn't
      p Foo  # => uninitialized constant M::N::Foo
    end
    p M::N.const_get :Foo  # => uninitialized constant M::N::Foo
    

    In both places, though, accessing Object level constants like Array is fine (thank god!). What’s going on is that Ruby maintains a list of “opened Module definitions”. If a constant has an explicit scope, say LookHereOnly::Foo, then only LookHereOnly and its included modules will be searched. If no scope is specified (like Foo in the example above), Ruby will look through the opened module definitions to find the constant Foo: M::N, then M and finally Object. The topmost opened module definition is always Object.

    So M::N.const_get :Foo is equivalent to accessing Foo when the opened classes are only M::N and Object, like in the last part of my example.

    I hope I got this right, coz I’m still confused by constant lookups myself 🙂

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

Sidebar

Related Questions

According to the documentation of the == operator in MSDN , For predefined value
According to the documentation in VB6 the Mid() function returns a variant, but Mid$()
According the Perl documentation on regexes : By default, the "^" character is guaranteed
According to documentation Default implementation does nothing. But... I throw exception from drawRect method
According to the documentation , .removeData() will cause the value of the property being
I'm trying to install dajax / dajaxice according to their documentation, which suggests that
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to the documentation, the decimal.Round method uses a round-to-even algorithm which is not
According to the documentation, a prepared query provides a significant performance benefit if you're
According to the documentation of java.util.Pattern , the POSIX character class \p{Graph} ( [:graph:]

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.