# How to unset a variable in ruby

**URL:** <https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869>\
**Category:** Ruby\
**Created:** [July 9, 2014, 2:16pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869 "2014-07-09T14:16:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [July 9, 2014, 2:16pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/1 "2014-07-09T14:16:37Z")

</div>

hi,

$a=23

def function

```
puts $a

```

end

Now I need to unset this variable, how would I do this? That means that  
variable no longer should be in use. Can anyone please provide me the  
solution for this?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 9, 2014, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/2 "2014-07-09T20:47:09Z")

</div>

$a = nil

Or if you don’t use a global variable, once it’s out of scope it will be  
garbage collected.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 10, 2014, 5:33am UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/3 "2014-07-10T05:33:57Z")

</div>

hi,

If you assign the value nil, then it’s still holding the object, $a or a  
still is in use, I would like to undefine the variable,which means If  
you use the variable again, program has to say “undefined local variable  
or method `a’ for main:Object (NameError)”, How would I do this?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 10, 2014, 4:45pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/4 "2014-07-10T16:45:49Z")

</div>

Why do you need to assigne a value to a global variable when you don’t  
need it later in your program? Such code really smells. You should  
rather find a better place for it (maybe a local or instance variable)  
in your code and let the garbage collector take care of it once it’s  
done its job.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 11, 2014, 3:20pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/5 "2014-07-11T15:20:56Z")

</div>

hi,

Yes as you say I could have used local variable, but I need the same  
variable within more than one function and that’s the reason I am in a  
need of deleting that variable after usage.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 12, 2014, 8:22am UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/6 "2014-07-12T08:22:26Z")

</div>

hi Damián M. González

Ok, thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [July 11, 2014, 6:44pm UTC](https://www.ruby-forum.com/t/how-to-unset-a-variable-in-ruby/230869/7 "2014-07-11T18:44:32Z")

</div>

The answer is that global variables by default have nil value. If you  
are specting Ruby to throw an error because of a global variable was  
never set you are thinking wrong, whatever call to a global variable  
will return nil if not defined, so don’t worry. If you want to free  
memory just assing nil to the global variable you don’t need anymore.
