class RuboCop::Cop::Style::Encoding

always - enforce encoding comment in all files
characters, otherwise report an offense
when_needed - only enforce an encoding comment if there are non ASCII
source file encoding. There are two style:
should support Ruby 1.9, since in 2.0+ utf-8 is the default
comment or not. This check makes sense only for code that
This cop checks whether the source file has a utf-8 encoding

def autocorrect(node)

def autocorrect(node)
  encoding = cop_config['AutoCorrectEncodingComment']
  if encoding && encoding =~ ENCODING_PATTERN
    lambda do |corrector|
      corrector.replace(node.pos, "#{encoding}\n#{node.pos.source}")
    end
  else
    fail "#{encoding} does not match #{ENCODING_PATTERN}"
  end
end

def encoding_line_number(processed_source)

def encoding_line_number(processed_source)
  line_number = 0
  line_number += 1 if processed_source[line_number].start_with?('#!')
  line_number
end

def investigate(processed_source)

def investigate(processed_source)
  return if processed_source.buffer.source.empty?
  line_number = encoding_line_number(processed_source)
  message = offense(processed_source, line_number)
  return unless message
  range = source_range(processed_source.buffer, line_number + 1, 0)
  add_offense(processed_source.tokens.first, range, message)
end

def offense(processed_source, line_number)

def offense(processed_source, line_number)
  line = processed_source[line_number]
  encoding_present = line =~ ENCODING_PATTERN
  ascii_only = processed_source.buffer.source.ascii_only?
  always_encode = style == :always
  if !encoding_present && (always_encode || !ascii_only)
    MSG_MISSING
  elsif !always_encode && ascii_only && encoding_present
    MSG_UNNECESSARY
  end
end