class Formatter

Public Class Methods

new(file) click to toggle source
# File lib/formatter.rb, line 2
def initialize(file)
  @file = file
  @temp = []
  temp_file
end

Public Instance Methods

format() click to toggle source
# File lib/formatter.rb, line 8
def format
  whitespace_remover
  empty_line_remover
  @new_file = File.open(@file, 'w')
  @new_file.write(@temp.join)
  @temp
end

Private Instance Methods

empty_line_remover() click to toggle source
# File lib/formatter.rb, line 35
def empty_line_remover
  result = []
  @temp.each_with_index do |line, index|
    result << line unless @temp[index - 1] == "\n" && line == "\n"
  end
  @temp = result
end
temp_file() click to toggle source
# File lib/formatter.rb, line 18
def temp_file
  @read_file = File.open(@file, 'r')
  @read_file.each do |line|
    @temp << line
  end
end
whitespace_remover() click to toggle source
# File lib/formatter.rb, line 25
def whitespace_remover
  @temp.each_with_index do |line, index|
    @temp[index] = if line.end_with?("\n")
                     line.gsub(/\s+\n$/, "\n")
                   else
                     line.gsub(/\s+$/, '')
                   end
  end
end