Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
String#replace_invalid_bytes that replaces invalid bytes in a string
# -*- coding: utf-8 -*-
class String
def replace_invalid_bytes(replace = nil)
if block_given?
if replace
warn 'block not used' if $VERBOSE
end
elsif replace.nil?
raise ArgumentError, 'an argument or a block must be given'
end
return self if valid_encoding?
if replace
r = String.try_convert(replace) or
raise TypeError, "can't convert #{replace.class} into String"
each_char.inject('') { |s, c|
s << (c.valid_encoding? ? c : r)
}
else
each_char.inject('') { |s, c|
s << (c.valid_encoding? ? c : yield(*c.bytes))
}
end
end
end
puts "あい\x80\xff".replace_invalid_bytes('[?]')
#=> あい[?][?]う
puts "あい\x80\xff".replace_invalid_bytes { |b| "<%02X>" % b }
#=> あい<80><FF>う
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment