Sha256: 0bf09b2da088697ff4342d93ac6d688038944d165fb4a57a26ed678c6dcb4928

Contents?: true

Size: 624 Bytes

Versions: 11

Compression:

Stored size: 624 Bytes

Contents

# encoding: utf-8

module TTY
  # A mixin to provide unicode support.
  module Unicode

    def utf8?(string)
      string.unpack('U*') rescue return false
      true
    end

    def clean_utf8(string)
      require 'iconv'
      if defined? ::Iconv
        converter = Iconv.new('UTF-8//IGNORE', 'UTF-8')
        converter.iconv(string)
      end
    rescue Exception
      string
    end

    if ''.respond_to?(:encode)
      def as_unicode
        yield
      end
    else
      def as_unicode
        old, $KCODE = $KCODE, 'U'
        yield
      ensure
        $KCODE = old
      end
    end
  end # Unicode
end # TTY

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tty-0.5.0 lib/tty/support/unicode.rb
tty-0.4.0 lib/tty/support/unicode.rb
tty-0.3.2 lib/tty/support/unicode.rb
tty-0.3.1 lib/tty/support/unicode.rb
tty-0.3.0 lib/tty/support/unicode.rb
tty-0.2.1 lib/tty/support/unicode.rb
tty-0.2.0 lib/tty/support/unicode.rb
tty-0.1.3 lib/tty/support/unicode.rb
tty-0.1.2 lib/tty/support/unicode.rb
tty-0.1.1 lib/tty/support/unicode.rb
tty-0.1.0 lib/tty/support/unicode.rb