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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:43:09+00:00 2026-06-08T06:43:09+00:00

As an experiment, I wrote some code that looks like class MyClass @var =

  • 0

As an experiment, I wrote some code that looks like

class MyClass

    @var = 100

    def hello
      p "hello"
    end

end

I know that if I do MyClass.new that @var is not defined on that object, rather I think this would define @var on the class MyClass.

Is there a practical usage to this?

  • 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-08T06:43:10+00:00Added an answer on June 8, 2026 at 6:43 am

    It does have a use: Class variables. The normal Ruby class variable implementation, @@, shares the same variable between a superclass and its subclasses:

    class A
      @@v = 0
      def self.v; @@v; end
      def self.v=(val); @@v=val; end
    end
    class B < A; end
    A.v #-> 0
    A.v= 3 #-> 3
    B.v #->3
    B.v= 42 #-> 42
    A.v #-> 42
    

    Obviously, this is pretty useless (except for the fact that, unlike class instance variables, class variables are also accessible directly in instance methods, as opposed to through self.class). But the same example with class instance variables:

    class A
      @v = 0
      def self.v; @v; end
      def self.v=(val); @v=val; end
    end
    class B < A; end
    A.v #-> 0
    A.v= 3 #-> 3
    B.v= 42 #-> 42
    A.v #-> 3
    

    Also, class instance variables can harness all of the metaprogramming already written for instance variables, like so:

    class Foo
      class << self
        attr_accessor :v #Uses a class instance variable
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some metaprogramming code such that: Inheriting from some class foo<c1,
I'm starting to experiment with CouchDB because it looks like the perfect solution for
I'm running some test to prove a concept and i just wrote this code
I started a little experiment today: I wrote a C++ class which depends on
I would like to write a wrapper around a custom function that takes some
I quickly wrote an interpreter for some sort of experimental programing language i came
I am trying to experiment this parameter in MapReduce and I have some question.
I'd like to experiment with NHaml . Can I use the NHaml view engine
I'm working on a project that is using code from an OpenSource project. One
As a summer learning experiment, I'm thinking of coding up a web front end

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.