Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #255 from ryenus/fix-6510

JRUBY-6510 fix String.encode!
  • Loading branch information...
commit 8a773ea56a9d2957af3c1866851b94c9f8424de7 2 parents 9e85bb2 + 7033a99
Charles Oliver Nutter headius authored
16 spec/regression/JRUBY-6510_hash_treat_as_encoding_spec.rb
View
@@ -0,0 +1,16 @@
+# encoding: UTF-8
+require 'rspec'
+
+describe 'JRUBY-6510: String.encode!' do
+ it 'should also accept a hash as the only argument' do
+ enc_dft_in = Encoding.default_internal
+ begin
+ Encoding.default_internal = 'ISO-8859-1'
+ s = "äöü"
+ s.encode!({:invalid => :replace, :undef => :replace})
+ s.encoding.name.should == 'ISO-8859-1'
+ ensure
+ Encoding.default_internal = enc_dft_in unless enc_dft_in.nil?
+ end
+ end
+end
27 src/org/jruby/RubyString.java
View
@@ -7399,11 +7399,22 @@ public IRubyObject encode_bang(ThreadContext context) {
}
@JRubyMethod(name = "encode!", compat = RUBY1_9)
- public IRubyObject encode_bang(ThreadContext context, IRubyObject enc) {
+ public IRubyObject encode_bang(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.runtime;
modify19();
+ Encoding toEncoding;
+ IRubyObject options;
- value = transcode(context, value, null, getEncoding(runtime, enc), runtime.getNil());
+ if (arg instanceof RubyHash) {
+ toEncoding = runtime.getDefaultInternalEncoding();
+ if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
+ options = arg;
+ } else {
+ toEncoding = getEncoding(runtime, arg);
+ options = runtime.getNil();
+ }
+
+ value = transcode(context, value, null, toEncoding, options);
return this;
}
@@ -7452,20 +7463,20 @@ public IRubyObject encode(ThreadContext context) {
@JRubyMethod(name = "encode", compat = RUBY1_9)
public IRubyObject encode(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.runtime;
- Encoding forceEncoding;
+ Encoding toEncoding;
IRubyObject options;
if (arg instanceof RubyHash) {
- forceEncoding = runtime.getDefaultInternalEncoding();
- if (forceEncoding == null) forceEncoding = runtime.getEncodingService().getLocaleEncoding();
- if (forceEncoding == null) return dup();
+ toEncoding = runtime.getDefaultInternalEncoding();
+ if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
+ if (toEncoding == null) return dup();
options = arg;
} else {
- forceEncoding = getEncoding(runtime, arg);
+ toEncoding = getEncoding(runtime, arg);
options = runtime.getNil();
}
- return runtime.newString(transcode(context, value, null, forceEncoding, options));
+ return runtime.newString(transcode(context, value, null, toEncoding, options));
}
@JRubyMethod(name = "encode", compat = RUBY1_9)
Please sign in to comment.
Something went wrong with that request. Please try again.