Search code examples
rubymetaprogramminglocal-variables

Undefine variable in Ruby


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.

Solution

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