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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:51:16+00:00 2026-05-10T16:51:16+00:00

How can I add an instance variable to a defined class at runtime ,

  • 0

How can I add an instance variable to a defined class at runtime, and later get and set its value from outside of the class?

I’m looking for a metaprogramming solution that allows me to modify the class instance at runtime instead of modifying the source code that originally defined the class. A few of the solutions explain how to declare instance variables in the class definitions, but that is not what I am asking about.

  • 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. 2026-05-10T16:51:17+00:00Added an answer on May 10, 2026 at 4:51 pm

    You can use attribute accessors:

    class Array   attr_accessor :var end 

    Now you can access it via:

    array = [] array.var = 123 puts array.var 

    Note that you can also use attr_reader or attr_writer to define just getters or setters or you can define them manually as such:

    class Array   attr_reader :getter_only_method   attr_writer :setter_only_method    # Manual definitions equivalent to using attr_reader/writer/accessor   def var     @var   end    def var=(value)     @var = value   end end 

    You can also use singleton methods if you just want it defined on a single instance:

    array = []  def array.var   @var end  def array.var=(value)   @var = value end  array.var = 123 puts array.var 

    FYI, in response to the comment on this answer, the singleton method works fine, and the following is proof:

    irb(main):001:0> class A irb(main):002:1>   attr_accessor :b irb(main):003:1> end => nil irb(main):004:0> a = A.new => #<A:0x7fbb4b0efe58> irb(main):005:0> a.b = 1 => 1 irb(main):006:0> a.b => 1 irb(main):007:0> def a.setit=(value) irb(main):008:1>   @b = value irb(main):009:1> end => nil irb(main):010:0> a.setit = 2 => 2 irb(main):011:0> a.b => 2 irb(main):012:0>  

    As you can see, the singleton method setit will set the same field, @b, as the one defined using the attr_accessor… so a singleton method is a perfectly valid approach to this question.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. You cannot create the "classic" kernel-mode drivers. However, starting… May 11, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer You can call CREATE Function near the beginning of your… May 11, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer Perhaps you've run into a bug which was fixed on… May 11, 2026 at 8:02 pm

Related Questions

I'm writing a game, and I want to model its different states (the Game
I want to send a UIImage to a server with socket. I'm using this
class Person(models.Model): name = models.CharField(max_length=100) class Entry(models.Model): text = models.CharField(max_length=100) person = models.ManyToManyField(Person, blank=True,
I'm trying to use custom container widgets in gtk.Builder definition files. As far as

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.