Permalink
Browse files

The default internal encoding cannot be set multiple times using -U a…

…nd -E.
  • Loading branch information...
ileitch committed May 21, 2012
1 parent f162836 commit d4f9cc7e5fcd51e39ae999a79e0e0e98d4a6f04f
View
@@ -319,13 +319,13 @@ def options(argv=ARGV)
end
else
options.on "-U", "Set Encoding.default_internal to UTF-8" do
- Encoding.default_internal = "UTF-8"
+ set_default_internal_encoding('UTF-8')
end
options.on "-E", "ENC", "Set external:internal character encoding to ENC" do |enc|
ext, int = enc.split(":")
Encoding.default_external = ext if ext and !ext.empty?
- Encoding.default_internal = int if int and !int.empty?
+ set_default_internal_encoding(int) if int and !int.empty?
end
end
@@ -478,6 +478,16 @@ def options(argv=ARGV)
end
end
+ def set_default_internal_encoding(encoding)
+ if @default_internal_encoding_set && Encoding.default_internal.name != encoding
+ raise RuntimeError, "Default internal encoding already set to '#{Encoding.default_internal.name}'."
+ else
+ @default_internal_encoding_set = true
+ Encoding.default_internal = encoding
+ end
+ end
+ private :set_default_internal_encoding
+
RUBYOPT_VALID_OPTIONS = "IdvwWrKT"
def handle_rubyopt(options)
@@ -0,0 +1,9 @@
+ruby_version_is "1.9" do
+ describe "ruby -E" do
+ it "raises a RuntimeError if used with -U" do
+ ruby_exe("p 1",
+ :options => '-Eascii:ascii -U',
+ :args => '2>&1').should =~ /RuntimeError/
+ end
+ end
+end
@@ -1,3 +1 @@
fails:ruby -U does not affect the source encoding
-fails:ruby -U raises a RuntimeError if used with -Eext:int
-fails:ruby -U raises a RuntimeError if used with -E:int

0 comments on commit d4f9cc7

Please sign in to comment.