在Ruby中未定义变量 [英] Undefine variable in Ruby
本文介绍了在Ruby中未定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我使用的是irb
,然后键入a = 5
.如何删除a
的定义,以便键入a
会返回NameError
?
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.
推荐答案
有 remove_instance_variable 和 remove_const 方法,但当前没有等效的局部变量.
There are remove_class_variable, remove_instance_variable and remove_const methods but there is currently no equivalent for local variables.
这篇关于在Ruby中未定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文