Sha256: 707e7de74bd2a19f969a29eadabb7dc0e2db1f4ce60cbce8f8343dcc6b0b78ae

Contents?: true

Size: 758 Bytes

Versions: 1

Compression:

Stored size: 758 Bytes

Contents

# encoding: utf-8

module Eightball

  def eightball(replacement_string = '?')
    string = self
    string = string.force_encoding(Encoding::UTF_8) if not_utf8?
    string = reencode(string, replacement_string)
    string = drop_bom(string)
    string
  end

  private

  def not_utf8?
    !self.valid_encoding? || self.encoding != Encoding::UTF_8
  end

  def reencode(string, replacement_string)
    string = string.encode(Encoding::UTF_16, :invalid => :replace, :undef => :replace, :replace => replacement_string)
    string = string.encode(Encoding::UTF_8, :invalid => :replace, :undef => :replace, :replace => replacement_string)
    string
  end

  def drop_bom(string)
    string.sub(/^\xEF\xBB\xBF/, '')
  end

end

String.send(:include, Eightball)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eightball-0.2.2 lib/eightball.rb