Class: String

Inherits:
Object show all
Defined in:
lib/ruby_peter_v/truncate_utf8.rb

Instance Method Summary collapse

Instance Method Details

#truncate_utf8(max_byte_size) ⇒ Object



3
4
5
# File 'lib/ruby_peter_v/truncate_utf8.rb', line 3

def truncate_utf8(max_byte_size)
  self.bytes.to_a[0...max_byte_size].pack('c*').force_encoding('UTF-8').encode("UTF-16BE", :invalid => :replace, :replace =>"").encode("UTF-8")
end