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

  • Home
  • SEARCH
  • 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 6890519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:17:32+00:00 2026-05-27T06:17:32+00:00

class A def set(v) @@v = v end def put puts @@v end end

  • 0
class A
  def set(v)
    @@v = v
  end
  def put
    puts @@v
  end
end

class B < A
end
class C < A
end

B.new.set 'b'
B.new.put # => b
C.new.set 'c'
C.new.put # => c
B.new.put # => c

Why? And how should I write this to have ‘b’ in last B.new.put?

  • 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-27T06:17:32+00:00Added an answer on May 27, 2026 at 6:17 am

    Here is a nice article on the subject – Class and Instance Variables In Ruby.

    Basically, what you can do is:

    class A
      class << self
        attr_accessor :class_var
      end
    
      def set_class_var(value)
        self.class.class_var = value
      end
    
      def get_class_var
        self.class.class_var
      end
    end
    
    class B < A; end
    
    A.class_var = 'a'
    B.class_var = 'b'
    puts A.class_var # => a
    puts B.class_var # => b
    
    A.new.set_class_var 'aa'
    B.new.set_class_var 'bb'
    puts A.new.get_class_var # => aa
    puts B.new.get_class_var # => bb
    

    To understand it you should think about A as an instance of Class class (and that’s how it is in Ruby). But every object in Ruby has its own singleton class that stores object-specific stuff like methods defined on object itself:

    a = A.new
    def a.foo
      puts 'foo'
    end
    

    In that case foo is method defined only for a object and not for every instance of A class. And another way to define method in object’s singleton class is like that:

    class << a # open a's singleton class
      def bar  # define method that will be available only on 'a' object
        puts 'bar'
      end
    end
    

    In the first code snippet we use that approach to define class_var attribute accessor in the context of singleton class of our A class (it’s a bit tricky, so you need to think about it). As the result class itself has class_var variable as well as its descendant class B. The difference is that every one of them has its own class_var variable that do not interfere.

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

Sidebar

Related Questions

I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
class A def a puts 'in #a' end end class B < A def
class Foo def initialize(a) puts Hello #{a} end end module Bar def initialize(b) puts
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have something like this: class Vehicle def self.set_color(input) if %w{blue red green}.include?(input) input
I have controller methods that look like this: class TestController < ApplicationController def testAction
I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
class A def initialize @x = do_something end def do_something 42 end end How
class Observer def initialize(&block) instance_eval(&block) if block_given? end end I'm wondering what assumption is
class MyWriter: def __init__(self, stdout): self.stdout = stdout self.dumps = [] def write(self, text):

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.