# How can I add an instance variable to an object after the object was initialized?

**URL:** <https://www.ruby-forum.com/t/how-can-i-add-an-instance-variable-to-an-object-after-the-object-was-initialized/262019>\
**Category:** Development\
**Created:** [October 27, 2021, 10:51am UTC](https://www.ruby-forum.com/t/how-can-i-add-an-instance-variable-to-an-object-after-the-object-was-initialized/262019 "2021-10-27T10:51:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ped](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@ped](https://www.ruby-forum.com/u/ped)\
**Post date:** [October 27, 2021, 10:51am UTC](https://www.ruby-forum.com/t/how-can-i-add-an-instance-variable-to-an-object-after-the-object-was-initialized/262019/1 "2021-10-27T10:51:34Z")

</div>

I have an instance of a class with data and would like to extend it with some instance variables. Here is what I know:

```
class A

  attr_accessor :id
  attr_accessor :description

  def initialize(id, description = nil)
    @id = id
    @description = description
  end
  
  def methode1
    puts "Method 1"
  end  

end

a = A.new(1, 'This is a descriptipn')

```

I got the instances `a` from another code, so that i don’t have access to the initialization. I can only continue from here:

```
a.instance_variable_set(:@my_var_1, '')
a.instance_variable_set(:@my_var_2, [])

```

This works fine. But if I want to get the value , i have to use the following code:

```
puts a.instance_variable_get(:@my_var_1)

```

Unfortunately, the following code does not work.

```
puts a.my_var_1

```

This makes me wonder why I can’t access the instance variable in this way, since it is now part of my instance.

Another question: How can I add an element to `@my_var_2`?

```
a.my_var_2 << 'a'
# => undefined method `my_var_2' for #<A:0x000001a2ad128b48 @id=1, @description="This is a descriptipn", @my_var_1="Text", @my_var_2=[]>Text
(NoMethodError)
```

---

<div class="post-metadata">

**Author:** ![topos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@topos](https://www.ruby-forum.com/u/topos)\
**Post date:** [October 29, 2021, 4:56am UTC](https://www.ruby-forum.com/t/how-can-i-add-an-instance-variable-to-an-object-after-the-object-was-initialized/262019/2 "2021-10-29T04:56:05Z")

</div>

Hi,  
an instance variable is accessible inside the instantiated object. But it’s not a method.  
Thus you have to add a getter and a setter-method

```ruby
class A
  def my_var= v
    @my_var = y
  end

  def my_var
      @my_var
  end
end 

```

to achieve your goal.

This can be done through meta programming

you might checkout [dry initializer](https://dry-rb.org/gems/dry-initializer/3.0/)

---

<div class="post-metadata">

**Author:** ![bleak\_gravel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bleak\_gravel](https://www.ruby-forum.com/u/bleak_gravel)\
**Post date:** [August 29, 2024, 1:00pm UTC](https://www.ruby-forum.com/t/how-can-i-add-an-instance-variable-to-an-object-after-the-object-was-initialized/262019/4 "2024-08-29T13:00:53Z")

</div>

```auto
f = File.new("/tmp")

module AddP 
  def p=(pv)
     @p = pv
   end
   attr_reader :p
end

f.extend AddP
f.p = 2

puts f.p.to_s

```

Will print: **2**  
Alternatively:

```auto
f = File.new('/tmp')
f.define_singleton_method(:p=) {|value| @p = value }
f.p = 87
puts f.instance_eval("@p").to_s

```

Prints **87**
