Module: Readlines::Sort

Included in:
ReadDuc
Defined in:
lib/readlines/readlines/sort.rb

Instance Method Summary collapse

Instance Method Details

#sort_alphabetically_nowObject

Raises:

  • (Readlines::NotFoundError)


8
9
10
11
12
13
14
15
16
# File 'lib/readlines/readlines/sort.rb', line 8

def sort_alphabetically_now
    raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)

    content = ::File.read(@file_path)
    sorted_lines = content.lines.sort_by { |line| line.strip }
    updated_content = sorted_lines.join
    ::File.write(@file_path, updated_content)
    updated_content
end