class RuboCop::Cop::Style::AutoResourceCleanup
end
# …
Tempfile.open(‘temp’) do |f|
# good
f = Tempfile.open(‘temp’)
# bad
end
# …
File.open(‘file’) do |f|
# good
f = File.open(‘file’)
# bad
@example
resource cleanup.
accepting version of a method that does automatic
Checks for cases when you could use a block
def cleanup?(node)
def cleanup?(node) return true if node.block_argument? return false unless (parent = node.parent) parent.block_type? || !parent.lvasgn_type? end
def on_send(node)
def on_send(node) return if !file_open_method?(node) || cleanup?(node) current = node.receiver.source_range.begin.join(node.selector.end).source add_offense(node, message: format(MSG, current: current)) end