通过方法清除实例变量的良好命名约定是什么?实际值存储在一个数组中,方法过滤数组以找到值。清除实例变量的方法的命名约定
这里有方法,我已经有了:
class Foo
def initialize
# Array of objects
@data = []
end
# Get the variable or default value
def variable
@data.select { |o| o.attribute == 'Some Value' }.first || 'Default Value'
end
# Does the variable exist
def variable?
[email protected] { |o| o.attribute == 'Some Value' }.first
end
# Does this make sense?
def clear_variable!
# Delete the variable
end
end
还是应像delete_variable!
?
有趣的问题。我有一个关于它的问题:)什么? :P(我显然不知道Ruby,但这看起来很有趣)。 – Mzn
在Ruby中,您不能删除任何变量,而是可以清除它。 –
您需要调用'@ data'并将其放入'initialize'方法中,以便实际工作。 – tadman