这是我的代码,这是我的错误。我认为这里不需要更多的描述:
#terminal-error (error on object "= ->(object) do "
lib/form_object/base.rb:18:30: W: Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - object.
need_validation = ->(object) dodef valid?
valid_attributes = []
attributes.each do |attribute_name, _attributes|
attribute_set = self.class.attribute_set[attribute_name]
object = self[attribute_name]
need_validation = ->(object) do
(object.class < FormObject::Base || attribute_set.options[:validate]) && object.respond_to?(:valid?)
end
if need_validation.call(object)
valid_attributes << object.valid?
elsif object.is_a?(Array)
object.each do |nested|
valid_attributes << nested.valid? if need_validation.call(nested)
end
end
end
valid_attributes << super
valid_attributes.all?
end发布于 2018-04-20 13:14:02
这是来自运行的lint的警告,它检测到从外部作用域中隐藏(即隐藏)另一个局部变量的情况。
你有:
object = self[attribute_name]
need_validation = ->(object) do
(object.class < FormObject::Base || attribute_set.options[:validate]) && object.respond_to?(:valid?)
end因此,第一个变量object不能在lambda中引用,因为参数也称为object。
只需重命名lambda的参数,就可以删除此警告:
need_validation = ->(obj) do
(obj.class < FormObject::Base || attribute_set.options[:validate]) && obj.respond_to?(:valid?)
endhttps://stackoverflow.com/questions/49936866
复制相似问题