Undefine variable in Ruby

Peter picture Peter · Jan 6, 2010 · Viewed 21.9k times · Source

Let's say I'm using irb, and type a = 5. How do I remove the definition of a so that typing a returns a NameError?

Some context: later I want to do this:

context = Proc.new{}.binding
context.eval 'a = 5'
context.eval 'undef a'  # though this doesn't work.

Answer

mikej picture mikej · Jan 6, 2010

There are remove_class_variable, remove_instance_variable and remove_const methods but there is currently no equivalent for local variables.