How can you check if an instance variable is set when you have a symbol representing the instance variable?

huangapple go评论91阅读模式
英文:

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个方法来获取、设置、检查、列出和移除实例变量:

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:

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

huangapple
  • 本文由 发表于 2023年7月28日 05:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定