英文:
How can you check if an instance variable is set when you have a symbol representing the instance variable?
问题
我想使用remove_instance_variable(instance_variable)
,其中instance_variable
包含形式为:@some_variable
的符号,但首先我想验证实例变量(例如@some_variable
)是否存在。
defined? instance_variable
当然会返回"local-variable",所以对我没有帮助。
即使我可以获取instance_variable
的内容,要有效地使用defined? :@some_variable
,它将返回"expression",因为它告诉我该符号已定义(在这个上下文中,即使你以前从未使用过的符号也会返回"expression")。
如果我使用eval
,那么该符号会方便地替换为实际的实例变量,并且它会返回nil
或"instance-variable"。
eval("defined? #{instance_variable}")
但考虑到对eval
的强烈反感(当变量可能包含用户提交的值时,这当然是合理的),如果可能的话,我宁愿找到另一种解决方案。即使我彻底确保instance_variable
不可能包含任何用户提交的数据,每个看到我之后的代码的人都会停下来问"为什么在这里使用eval
?"。
是否有另一种方法来检查instance_variable
内的符号引用的实例变量是否已定义?
英文:
I want to use remove_instance_variable(instance_variable)
, where instance_variable
contains a Symbol of the form :@some_variable
, but first I want to verify that the instance variable (e.g. @some_variable
exists.
defined? instance_variable
will of course return "local-variable", so that does me no good.
Even if I could grab the contents of instance_variable
, to effectively use defined? :@some_variable
that would return "expression" because it's telling me that the symbol is defined. (All symbols, even which you've never used before, will return "expression" in this context.)
If I use eval
, then the symbol gets conveniently replaced by the actual instance variable, and it works, returning either nil
or "instance-variable".
eval("defined? #{instance_variable}")
But given the strong aversion towards eval
(which is of course justified in cases where the variable may contain user-submitted values), I'd rather find a different solution, if possible. Even if I thoroughly ensure that there's no possibility of instance_variable
containing any user-submitted data, everyone who looks at the code after me is going to stop and ask "why is eval
being used here?".
Is there another way to check if the instance variable referred to by the symbol inside instance_variable
is defined?
答案1
得分: 1
Object
实现了5个方法来获取、设置、检查、列出和移除实例变量:
Object#instance_variable_get
– 返回实例变量的值Object#instance_variable_set
– 设置实例变量的值Object#instance_variable_defined?
- 检查实例变量是否已定义Object#instance_variables
– 返回所有实例变量名称的数组Object#remove_instance_variable
– 移除实例变量
instance_variable_defined?
是你要寻找的方法:
name = :@foo
instance_variable_defined?(name) # => false
@foo = nil
instance_variable_defined?(name) # => true
这个方法还可以用于实现可能返回 nil
的值的记忆化:
def foo
return @foo if instance_variable_defined?(:@foo)
@foo = expensive_operation_that_could_return_nil
end
英文:
Object
implements 5 methods for getting, setting, checking, listing, and removing instance variables:
Object#instance_variable_get
– returns the value of an instance variableObject#instance_variable_set
– sets the value of an instance variableObject#instance_variable_defined?
- checks if an instance variable is definedObject#instance_variables
– returns an array of all instance variable namesObject#remove_instance_variable
– removes an instance variable
instance_variable_defined?
is what you are looking for:
name = :@foo
instance_variable_defined?(name) #=> false
@foo = nil
instance_variable_defined?(name) #=> true
The method is also useful for implementing memoization of values that could be nil
:
def foo
return @foo if instance_variable_defined?(:@foo)
@foo = expensive_operation_that_could_return_nil
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论