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 7669879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:41:42+00:00 2026-05-31T15:41:42+00:00

What’s the difference between: class A class B end end and class A end

  • 0

What’s the difference between:

class A
  class B
  end
end

and

class A
end

class A::B
end

Update: These 2 approaches are not exactly the same.

In the second approach, B doesn’t have access to constants defined in A.

Also, as Matheus Moreira correctly stated, in the second approach, A must be defined before A::B can be defined.

What other differences are there?

  • 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-31T15:41:44+00:00Added an answer on May 31, 2026 at 3:41 pm

    In Ruby, modules and classes are instances of the Module and Class classes, respectively. They derive their names from the constant they are assigned to. When you write:

    class A::B
      # ...
    end
    

    You are effectively writing:

    A::B ||= Class.new do
      # ...
    end
    

    Which is valid constant assignment syntax, and assumes that the A constant has been properly initialized and that it refers to a Module or a Class.

    For example, consider how classes are usually defined:

    class A
      # ...
    end
    

    What is effectively happening is this:

    Object::A ||= Class.new do
      # ...
    end
    

    Now, when you write:

    class A
      class B
        # ...
      end
    end
    

    What actually happens looks like this:

    (Object::A ||= Class.new).class_eval do
      (A::B ||= Class.new).class_eval do
        # ...
      end
    end
    

    Here’s what is happening, in order:

    1. A new Class instance is asssigned to the A constant of Object, unless it was already initialized.
    2. A new Class instance is asssigned to the B constant of A, unless it was already initialized.

    This ensures the existence of all outer classes before attempting to define any inner classes.

    There is also a change in scope, which allows you to directly access A‘s constants. Compare:

    class A
      MESSAGE = "I'm here!"
    end
    
    # Scope of Object
    class A::B
      # Scope of B
      puts MESSAGE  # NameError: uninitialized constant A::B::MESSAGE
    end
    
    # Scope of Object
    class A
      # Scope of A
      class B
        # Scope of B
        puts MESSAGE  # I'm here!
      end
    end
    

    According to this blog post, the Ruby core team calls the “current class” the cref. Unfortunately, the author does not elaborate, but as he notes, it is separate from the context of self.


    As explained here, the cref is a linked list that represents the nesting of modules at some point in time.

    The current cref is used for constant and class variable lookup and
    for def, undef and alias.


    As the others have stated, they are different ways of expressing the same thing.

    There is, however, a subtle difference. When you write class A::B, you assume that the A class has already been defined. If it has not, you will get a NameError and B will not be defined at all.

    Writing properly nested modules:

    class A
      class B
      end
    end
    

    Ensures the A class exists before attempting to define B.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.