Resource leak Medium

Allocated resources are not released properly. This can slow down or crash your system. They must be closed along all paths to prevent a resource leak.

Detector ID
ruby/resource-leak@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1def file_reading_noncompliant(filename)
2  # Noncompliant: File hasn't been closed
3  file = File.open(filename, 'r')
4  contents = file.read
5  puts contents
6end

Compliant example

1def file_reading_compliant(filename)
2  # Compliant: File has been closed after read
3  File.open(filename, 'r') do |file|
4    file.each_line do |line|
5      puts line
6    end
7  end
8end