Sha256: fa43344124ab43e524ca0404a869987575273343dc03e3dc3c0aee2e0b46f7e9

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

require 'charlock_holmes' unless defined? CharlockHolmes

class String
  # Attempt to detect the encoding of this string
  #
  # Returns: a Hash with :encoding, :language, :type and :confidence
  def detect_encoding(hint_enc=nil)
    detector = CharlockHolmes::EncodingDetector.new
    detector.detect(self, hint_enc)
  end

  # Attempt to detect the encoding of this string, and return
  # a list with all the possible encodings that match it.
  #
  # Returns: an Array with zero or more Hashes,
  #          each one of them with with :encoding, :language, :type and :confidence
  def detect_encodings(hint_enc=nil)
    detector = CharlockHolmes::EncodingDetector.new
    detector.detect_all(self, hint_enc)
  end

  if method_defined? :force_encoding
    # Attempt to detect the encoding of this string
    # then set the encoding to what was detected ala `force_encoding`
    #
    # Returns: self
    def detect_encoding!(hint_enc=nil)
      if detected = self.detect_encoding(hint_enc)
        self.force_encoding(detected[:ruby_encoding]) if detected[:ruby_encoding]
      end
      self
    end
  end
end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
charlock_holmes-0.7.9 lib/charlock_holmes/string.rb
static_holmes-0.7.11 lib/charlock_holmes/string.rb
static_holmes-0.7.10 lib/charlock_holmes/string.rb
static_holmes-0.7.9 lib/charlock_holmes/string.rb
charlock_holmes-0.7.8 lib/charlock_holmes/string.rb
static_holmes-0.7.8 lib/charlock_holmes/string.rb
static_holmes-0.7.7 lib/charlock_holmes/string.rb
charlock_holmes-0.7.7 lib/charlock_holmes/string.rb
charlock_holmes-0.7.6 lib/charlock_holmes/string.rb
charlock_holmes-0.7.5 lib/charlock_holmes/string.rb
charlock_holmes-0.7.4 lib/charlock_holmes/string.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/charlock_holmes-0.7.3/lib/charlock_holmes/string.rb
charlock_holmes-0.7.3 lib/charlock_holmes/string.rb
charlock_holmes-0.7.2 lib/charlock_holmes/string.rb