# Undefine instance var & \[\] (three questions)

**URL:** <https://www.ruby-forum.com/t/undefine-instance-var-three-questions/131664>\
**Category:** Ruby\
**Created:** [March 12, 2008, 10:29am UTC](https://www.ruby-forum.com/t/undefine-instance-var-three-questions/131664 "2008-03-12T10:29:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 12, 2008, 10:29am UTC](https://www.ruby-forum.com/t/undefine-instance-var-three-questions/131664/1 "2008-03-12T10:29:12Z")

</div>

Hi.

Iam not very deep in ruby. Please answer:

1. is the attr (method) slower than instance\_variable\_get/set ?
2. How can I define [] for instance\_var\_get/set (like in struct) ?
3. Is there a way to undefine @variable. I assign nil to them, but in  
to\_yaml output they are still there (even if empty). Is there way to  
undefine them?

Thanks. FP.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 12, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/undefine-instance-var-three-questions/131664/2 "2008-03-12T12:12:45Z")

</div>

2008/3/12, Frantisek P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi.
> 
> Iam not very deep in ruby. Please answer:
> 
> 1. is the attr (method) slower than instance\_variable\_get/set ?

They serve different purposes. attr defines accessor methods  
and the other allows access to instance variables via computed  
variable names. So I’m not exactly sure what the purpose of the  
speed comparison is.

Well, a let’s benchmark anyway:

```
> require "benchmark"
> class Foo
> attr :a, true
> def initialize
> @a = :a
> end
> end
=> nil
> f = Foo.new
=> #<Foo:0xb7adeb18 @a=:a>
> Benchmark.bm { |x| x.report { 1_000_000.times { f.a } } }
      user system total real
  0.560000 0.090000 0.650000 ( 0.656214)
=> true
> Benchmark.bm { |x| x.report { 1_000_000.times {

```

f.instance\_variable\_get :@a } } }  
user system total real  
0.590000 0.100000 0.690000 ( 0.705093)  
=\> true  
\> Benchmark.bm { |x| x.report { 1\_000\_000.times { f.a = 1 } } }  
user system total real  
0.590000 0.090000 0.680000 ( 0.698388)  
=\> true  
\> Benchmark.bm { |x| x.report { 1\_000\_000.times {  
f.instance\_variable\_set :@a, 1 } } }  
user system total real  
0.650000 0.090000 0.740000 ( 0.759085)  
=\> true

Conclusion: The differences are noise.

> 1. How can I define [] for instance\_var\_get/set (like in struct) ?

```
class Foo
  def [](attr_name)
    instance_variable_get :"@#{attr_name}"
  end
  def []=(attr_name, value)
    instance_variable_set :"@#{attr_name}", value
  end
end

```

Though using [] and []= will definitely be slower than using  
classic accessor methods because we have to construct  
the instance variable name for every access.

> 1. Is there a way to undefine @variable. I assign nil to them, but in  
> to\_yaml output they are still there (even if empty). Is there way to  
> undefine them?

“ri instance\_variable” reveals Object#remove\_instance\_variable.

Stefan

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [March 12, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/undefine-instance-var-three-questions/131664/3 "2008-03-12T12:41:12Z")

</div>

On Wed, Mar 12, 2008 at 12:11 PM, Stefan L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. Is there a way to undefine @variable. I assign nil to them, but in  
> > to\_yaml output they are still there (even if empty). Is there way to  
> > undefine them?
> 
> “ri instance\_variable” reveals Object#remove\_instance\_variable.

I found it there and tried a bit in irb:

class Foo  
attr :bar  
def initialize  
@bar = “Baba”  
end  
end

foo = Foo.new #=\> #\<Foo:0x2ae813c @bar=“Baba”\>  
foo.instance\_eval {remove\_instance\_variable :@bar } #=\> “Baba”  
foo #=\> #Foo:0x2ae813c @bar is removed
