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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:21:31+00:00 2026-06-17T00:21:31+00:00

You can create instance variables of a class and define their accessors as singleton

  • 0

You can create instance variables of a class and define their accessors as singleton methods of that class.
But class variables like @@var behave strangely, especially when inherited. Are there any use cases of it?

  • 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-17T00:21:33+00:00Added an answer on June 17, 2026 at 12:21 am

    I interpret what you mean by strange as the fact that class variables are not different among each class but are shared among the inheritance hierarchy of classes.

    As you mention and is also pointed out in Jörg W Mittag’s answer to this question, it you wanted a variable to be consistent withing a class but differ outside of the class, then you can use a class instance variable on that class. That means, class variables should behave differently from that if it has any reason to exist. And class variables are indeed shared among classes within the inheritance hierarchy.

    class A
      @@foo = 3
    end
    class B < A
      puts @@foo
    end
    # => 3
    

    Probably, it may be useful when you want to share something that are common to classes in the hierarchy. For example, suppose you have some feature in a class that would be used in the classes that inherit this class. Suppose that that feature is dependent on a parameter, and you want that parameter change to be consistent among the classes that inherit that feature. Then, class variable will be useful.

    For example, suppose you have this method:

    class Array
      def join_with_delimiter
        join(@@delimiter)
      end
    end
    class A < Array; end
    

    By setting @@delimiter once, you can expect consistent behavior across Array and A.

    class Array; @@delimiter = "*" end
    Array.new([:a, :b, :c]).join_with_delimiter # => "a*b*c"
    A.new([:a, :b, :c]).join_with_delimiter # => "a*b*c"
    
    class A; @@delimiter = "#" end
    Array.new([:a, :b, :c]).join_with_delimiter # => "a#b#c"
    A.new([:a, :b, :c]).join_with_delimiter # => "a#b#c"
    

    If you do that with a class instance variable, you would not get consistent behavior:

    class Array
      def self.delimiter; @delimiter end
      def join_with_delimiter
        join(self.class.delimiter)
      end
    end
    class A < Array; end
    
    class Array; @delimiter = "*" end
    Array.new([:a, :b, :c]).join_with_delimiter # => "a*b*c"
    A.new([:a, :b, :c]).join_with_delimiter # => "abc"
    
    class A; @delimiter = "#" end
    Array.new([:a, :b, :c]).join_with_delimiter # => "a*b*c"
    A.new([:a, :b, :c]).join_with_delimiter # => "a#b#c"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we able to create two instance of log4j in class one to write
I can create database manually by going to cpanel. But I'd like to create
So I can create Django model like this: from django.db import models class Something(models.Model):
Is there an equivalent of selectors for classes? How can I create a instance
Can you elaborate what goes behind the scene when we create DirectoryEntry instance? Code
I have a class that I can have many instances of. Inside it creates
In c++ you can create new instances of a class on both the heap
I'm feeling very stupid, but...what is the right way to init instance variables in
Recently I've been learning how to create methods within classes so that I only
When I define a variable in a class, every time I declare an instance

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.