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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:28:26+00:00 2026-06-18T22:28:26+00:00

Regarding: class Test class << self def hi puts Hi there end end I

  • 0

Regarding:

class Test
  class << self
    def hi
      puts "Hi there"
    end
end

I came up with following image in my head:

Since everything is an object in Ruby, classes themselves are objects of class Class. By calling class << self you open up Class definition from the inside of Test and inject few instance methods. Since Test is an instance of Class, you can call those methods same way you call instance methods on your objects: Test.hi.

Following is the pseudo code which helps to visualise my previous sentence:

class Class
    def hi
        puts “Hi there”
    end
end

Test = Class.new(class Test
end)
Test.hi

Am I getting this right?

  • 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-06-18T22:28:27+00:00Added an answer on June 18, 2026 at 10:28 pm

    Suppose we have an object obj of class A. At this point, the ancestor hierarchy of obj‘s class is:

    [A, ...]
    

    What class << obj; ... end does is that it creates a class B whose only instance is obj, and puts it in the ancestor hierarchy of obj so that the ancestor hierarchy of the obj‘s class becomes:

    [B, A, ...]
    

    If you write class << self; ... end within the context of Test, then the body of it will be a class whose sole instance is Test. If you define an instance method hi within that body, then that will apply to instances of that class, which is Test. Hence you will be able to do Test.hi.

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

Sidebar

Related Questions

I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
I have a class like the following: class A: def __init__(self, arg1, arg2, arg3):
Regarding the following C++ program: class Base { }; class Child : public Base
Is there a way to know in a JUnit 4 test Class, if the
Quick question regarding __callStatic() in PHP; class Test{ public static function __callStatic($method, $arguments){ echo
I have an issue regarding Sendkeys Class, as i want to use this class
I've looked at a lot of questions on SO and google regarding sub-class mappings
i have a question regarding the AsyncTask class in android, and why it is
Just a general question regarding the difference between extending a class and implementing it.
I am thinking about some details regarding stringByReplacingOccurrencesOfString:withString: in NSString class According to document.

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.