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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:34:20+00:00 2026-05-26T12:34:20+00:00

Recently, I was investigating into some details about classes in Ruby, and was confused

  • 0

Recently, I was investigating into some details about classes in Ruby, and was confused by class definition.

In Ruby, the class definition is as follows,

class A
    def self.my_method
    end
end

and it’s the same as

class A
    class << self
        def my_method
        end
    end
end

then I was confused. For the 1st case, self can be regarded as a pointer to currently using object, and current class of the context is A. And the method looking-up is done recursively. But my question is, what does def do? How does it change the current object and context? The problem is same for the 2nd case. How does the description like class << self change the current object and context?

And another question. As far as I know, all Class objects are obeying design patterns like fly-weight, since they share the same Class object with same definition. Then the eigenclass became confusing. Since the def in a eigenclass actually defines a method with Class object, how can it be related to “def self.*”?

It looks too complicated from the outside, and I may need the design details of Ruby.

  • 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-26T12:34:21+00:00Added an answer on May 26, 2026 at 12:34 pm
    class A
      # self here is A
      def aa
        # self here is the instance of A who called this method
      end
      class << self
        # self is the eigenclass of A, a pseudo-class to store methods to a object.
        # Any object can have an eigenclass.
        def aa
          # self is the pseudo-instance of the eigenclass, self is A.
        end
      end
    end
    

    This is the same:

    class A
      def self.aa
      end
    end
    

    And this:

    class << A
      def aa
      end
    end
    

    And also this:

    def A.aa
    end
    

    You can open the eigenclass of anything:

    hello = "hello"
    class << hello
      def world
        self << " world"
      end
    end
    hello.world #=> "hello world"
    "my".world  #=> NoMethodError
    

    An eigenclass is actually an instance of Class, it has it own eigenclass too.

    “If it appears to be too confusing, just remember that Class is an object and that Object is a class.”

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

Sidebar

Related Questions

I've been investigating frame breaking code recently and have come across some really bizarre
I have recently been doing a bit of investigation into the different types of
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently, I started changing some of our applications to support MS SQL Server as
Alright so we had a problem recently In reporting services some of the String
Recently I came across some strange behaviour of my application. It has been developed
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
I was recently asked about alternatives to Coverity Prevent for a code base that
I recently did some performance testing and analysis of an ASP.NET application using out-of-process
I recently did some work modifying a Python gui app that was using wxPython

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.