Actions
Feature #639
closedNew String#encode_internal method
Feature #639:
New String#encode_internal method
Status:
Closed
Assignee:
-
Target version:
-
Description
=begin
Now that we have default_internal, I think there needs to be an easy way to check that a string is that encoding and if not transcode it. This is especially needed in libraries and other methods which are expected to return strings in defult_internal encoding if it is set.
Suggested implementation:
class String
def encode_internal
intern = Encoding.default_internal
# Do nothing if default_internal not set
return self unless intern
# Do nothing if already in default_internal encoding
return self if encoding == intern
# Just use "force_encoding" if compatible eg: ASCII/UTF-8
return force_encoding(intern) if Encoding.compatible(self, intern) == intern
# Otherwise transcode
encode(intern)
end
end
=end
Actions