Class: MtLineEraser

Inherits:
Object
  • Object
show all
Defined in:
lib/mt_line_eraser.rb

Class Method Summary collapse

Class Method Details

.erase_empty_lines_in_file(file_path) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/mt_line_eraser.rb', line 20

def erase_empty_lines_in_file(file_path)
  original_file = File.open(file_path, 'r')
  clone_file    = File.open("#{file_path}.clone", 'w')
  original_file.readlines.each do |line|
    clone_file.write(line) if line.gsub(/^[\s|\n]*/, '') != ''
  end
  [original_file, clone_file].map(&:close)
  File.rename(clone_file, original_file)
end

.erase_in(file_or_dir_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mt_line_eraser.rb', line 5

def erase_in(file_or_dir_path)
  return 'Invalid File path' unless File.exist?(file_or_dir_path)

  files_to_be_processed = if File.file?(file_or_dir_path)
                            [file_or_dir_path]
                          else
                            Dir["#{file_or_dir_path}/**/*.*"]
                          end
  files_to_be_processed.each do |_file|
    erase_empty_lines_in_file(_file)
    p "Erased all the empty lines from #{_file}"
  end
  nil
end