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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:57:59+00:00 2026-05-30T18:57:59+00:00

I think I’m going a bit crazy when trying to understand instance variables in

  • 0

I think I’m going a bit crazy when trying to understand instance variables in Ruby. My only aim here is to make sure that every object created for a given class has a variable with a predetermined value without writing an initialize method for that class. Something like:

class Test

  @my = []

  attr_accessor :my

end
t = Test.new
t.my # I want [] but this shows nil

Is it possible to achieve this without touching initialize ? Thanks.

EDIT: To clarify, I’m writing some piece of code which will be executed similar to attr_accessor in the sense that it’ll add an instance variable to the class in which it is executed. If I write my own initialize, I will end up clobbering the one written by the user.

  • 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-30T18:58:00+00:00Added an answer on May 30, 2026 at 6:58 pm

    What you are doing is defining an instance variable on the class level (Since classes are instances of the Class class, this works just fine).

    And no, there is no way around initialize.

    Edit: You have a little misconception in your edit. attr_accessor doesn’t add an instance variable to the class. What it does, literally, is this (using your example of my):

    def my; @my; end
    def my=(value); @my = value; end
    

    It doesn’t actively create/initialize any instance variable, it just defines two methods. And you could very well write your own class method that does similar things, by using define_method.

    Edit 2:

    To further illustrate how one would write such a method:

    class Module
      def array_attr_accessor(name)
        define_method(name) do
          if instance_variable_defined?("@#{name}")
            instance_variable_get("@#{name}")
          else
            instance_variable_set("@#{name}", [])
          end
        end
    
        define_method("#{name}=") do |val|
          instance_variable_set("@#{name}", val)
        end
      end
    end
    
    
    class Test
      array_attr_accessor :my
    end
    
    t = Test.new
    t.my # => []
    t.my = [1,2,3]
    t.my # => [1, 2, 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think most people here understand the importance of fully automated builds. The problem
I think instance variables are simple data types like int or double. Everything that
I think I'm missing something basic here. Why is the third IF condition true?
I think I'm missing something basic here. Any explanation or pointers to previously asked
I think it's called embossed button.. Is it possible to make one from code?
Think of a pointer-datatype, for instance to a floating-pointer number. typedef float* flPtrt; How
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I
I think the file that is produced is an .asm file, any idea how
I think most people know how to do this via the GUI (right click

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.