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
6
7
8
9
# File 'lib/ruby_peter_v/truncate_utf8.rb', line 3

def truncate_utf8(max_byte_size)
  cut_off = self.bytes.to_a[0...max_byte_size].pack('c*').force_encoding('UTF-8')
  while ! cut_off.valid_encoding? do
    cut_off = cut_off[0..-2]
  end
  cut_off
end