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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:18:23+00:00 2026-05-22T22:18:23+00:00

class << Awesomeness What is this << for? I searched, but the results only

  • 0
class << Awesomeness

What is this << for? I searched, but the results only tell me about string concatenation…

  • 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-22T22:18:24+00:00Added an answer on May 22, 2026 at 10:18 pm

    While it’s true that class << something is the syntax for a singleton class, as someone else said, it’s most often used to define class methods within a class definition. But these two usages are consistent. Here’s how.

    Ruby lets you add methods to any particular instance by doing this:

    class << someinstance
      def foo
        "Hello."
      end
    end
    

    This adds a method foo to someinstance, not to its class but to that one particular instance. (Actually, foo is added to the instance’s “singleton class,” but that’s more or less an implementation quirk.) After the above code executes, you can send method foo to someinstance:

    someinstance.foo   => "Hello."
    

    but you can’t send foo to other instances of the same class. That’s what << is nominally for. But people more commonly use this feature for syntactic gymnastics like this:

    class Thing
      def do_something
      end
    
      class << self
        def foo
          puts "I am #{self}"
        end
      end
    end
    

    When this code — this class definition — executes, what is self? It’s the class Thing. Which means class << self is the same as saying “add the following methods to class Thing.” That is, foo is a class method. After the above completes, you can do this:

    t = Thing.new
    t.do_something  => does something
    t.class.foo     => "I am Thing"
    t.foo           => NoMethodError: undefined method `foo'
    

    And when you think about what << is doing, it all makes sense. It’s a way to append to a particular instance, and in the common case, the instance being appended to is a class, so the methods within the block become class methods.

    In short, it’s a terse way to create class methods within a class definition block. Another way would be to do this:

    class Thing
      def self.foo
        # ...
      end
    end
    

    Same thing. Your example is actually a syntax error, but if you understand how << is used with instances and the class keyword, you’ll know how to correct it.

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

Sidebar

Related Questions

After reading about the System.Diagnostics.Contracts.Contract static class that has been influenced by the awesomeness
Here's the Griddler.java: package com.pic2griddler.awesomeness; public class Griddler { public String id, status, name,
class example: def exampleMethod(self): aVar = 'some string' return aVar In this example, how
class A: pass def b(self): print('b') A.b = b a = A() At this
class Test { public static void main(String[] args) throws Exception { Test t =
class hydra { var $dbcon; function __construct($ezSQL){ $this -> dbcon=$ezSQL; } } class user
class Item { private int address; private String itemString; public Item(String item) { separate(item);
class Program { static void Main(string[] args) { B foo = new B(); foo.DoWork();
class TestMe{ public static void main (String args[]){ String s3; System.out.print(s3); } } why
class Node { public: template<class T> T* GetComponent() { return new T(this); // actual

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.