class RuboCop::Cop::Style::OpenStructUse
allow(test_double).to receive(:a).and_return(‘b’)
test_double = double
# good (assumes test using rspec-mocks)
test_double = OpenStruct.new(a: ‘b’)
# bad
point = { x: 0, y: 1 }
# also good
point = Point.new(0, 1)
Point = Struct.new(:x, :y)
# good
point = OpenStruct.new(x: 0, y: 1)
# bad
@example
—–
end
end
OpenStruct.new # resolves to MyNamespace::OpenStruct
def new_struct
end
class OpenStruct # not the OpenStruct we’re looking for
module MyNamespace
—–
[source,ruby]
use of a hand-rolled ‘OpenStruct` type would be considered an offense:
Note that this cop may flag false positives; for instance, the following legal
@safety
to be used for performance, version compatibility, and potential security issues.
Flags uses of `OpenStruct`, as it is now officially discouraged
def custom_class_or_module_definition?(node)
def custom_class_or_module_definition?(node) parent = node.parent parent.type?(:class, :module) && node.left_siblings.empty? end
def on_const(node)
def on_const(node) return unless uses_open_struct?(node) return if custom_class_or_module_definition?(node) add_offense(node) end