Topic: How to 'unset' a value in Ruby

I've been programming PHP for a long time, Ruby and RoR for a much shorter time, please excuse me ignorance.  I can't seem to find an answer in the ruby docs for this:

How do I unset a value (variable, hash value, array value, etc) in Ruby?

In PHP I would do something like this:

unset($foo)
unset($foo['bar'])

Is there some equivalent expression in Ruby?

Last edited by trevorrowe (2007-02-17 13:15:01)

Re: How to 'unset' a value in Ruby

http://www.brpreiss.com/books/opus8/html/page415.html

You can set it to nil if you want to save on memory but there's (intentionally) no way to actually remove an object.  Ruby's garbage collector will take care of unused objects eventually though - so you don't have to worry about it much.

Re: How to 'unset' a value in Ruby

Thank you for the link and answer, answered my question!

Re: How to 'unset' a value in Ruby

If you want to remove something from an array or hash you can do this:

arr.delete(obj)
arr.delete_at(index)
hash.delete(key)

There's also a few more cooler ways to do it involving blocks if you need to do a bulk type delete.

Railscasts - Free Ruby on Rails Screencasts