Project

General

Profile

Actions

Feature #639

closed

New String#encode_internal method

Added by mike (Michael Selig) over 15 years ago. Updated about 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:19298]

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

Also available in: Atom PDF

Like0
Like0Like0Like0