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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:30:16+00:00 2026-05-14T02:30:16+00:00

What does class << self do in Ruby ?

  • 0

What does class << self do in 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-14T02:30:17+00:00Added an answer on May 14, 2026 at 2:30 am

    First, the class << foo syntax opens up foo‘s singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object.

    a = 'foo'
    class << a
      def inspect
        '"bar"'
      end
    end
    a.inspect   # => "bar"
    
    a = 'foo'   # new object, new singleton class
    a.inspect   # => "foo"
    

    Now, to answer the question: class << self opens up self‘s singleton class, so that methods can be redefined for the current self object (which inside a class or module body is the class or module itself). Usually, this is used to define class/module (“static”) methods:

    class String
      class << self
        def value_of obj
          obj.to_s
        end
      end
    end
    
    String.value_of 42   # => "42"
    

    This can also be written as a shorthand:

    class String
      def self.value_of obj
        obj.to_s
      end
    end
    

    Or even shorter:

    def String.value_of obj
      obj.to_s
    end
    

    When inside a function definition, self refers to the object the function is being called with. In this case, class << self opens the singleton class for that object; one use of that is to implement a poor man’s state machine:

    class StateMachineExample
      def process obj
        process_hook obj
      end
    
    private
      def process_state_1 obj
        # ...
        class << self
          alias process_hook process_state_2
        end
      end
    
      def process_state_2 obj
        # ...
        class << self
          alias process_hook process_state_1
        end
      end
    
      # Set up initial state
      alias process_hook process_state_1
    end
    

    So, in the example above, each instance of StateMachineExample has process_hook aliased to process_state_1, but note how in the latter, it can redefine process_hook (for self only, not affecting other StateMachineExample instances) to process_state_2. So, each time a caller calls the process method (which calls the redefinable process_hook), the behaviour changes depending on what state it’s in.

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

Sidebar

Related Questions

Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var
Possible Duplicate: class << self idiom in Ruby I have a quick Ruby question.
I know what class << self does. But what is the underlying mechanism of
Possible Duplicates: class << self idiom in Ruby Can someone please explain class <<
Possible Duplicate: What does ||= mean in Ruby? class Person attr_accessor :name class <<
I am a NOOB trying to understand some code. What does this self.class.current_section do?
What role does SSLSocketFactory class in java play when using HttpsURLConnection ? The java
Class DVD inherits class Media, has one more variable than the base class does.
can you please help me with this issue the String class does not have
Can't find the answer to my question in MSND: Does Timer class guarantees that

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.