From 4d99577d8964b85d417c0a3dac8ea80c3a843770 Mon Sep 17 00:00:00 2001 From: kiichi Date: Mon, 15 Dec 2014 22:41:12 +0900 Subject: [PATCH] Fix GH-2182 on jruby-1_7 --- core/src/main/java/org/jruby/RubyStruct.java | 9 ++++++++- ..._struct_inspect_has_ascii_encoding_spec.rb | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 spec/regression/GH-2182_struct_inspect_has_ascii_encoding_spec.rb diff --git a/core/src/main/java/org/jruby/RubyStruct.java b/core/src/main/java/org/jruby/RubyStruct.java index 74e4117719c7..a39d8d066f4f 100644 --- a/core/src/main/java/org/jruby/RubyStruct.java +++ b/core/src/main/java/org/jruby/RubyStruct.java @@ -556,7 +556,14 @@ private IRubyObject inspectStruct(final ThreadContext context) { // FIXME: MRI has special case for constants here buffer.append(RubyString.objAsString(context, member.eltInternal(i)).getByteList()); buffer.append('='); - buffer.append(inspect(context, values[i]).getByteList()); + RubyString valueInspect = inspect(context, values[i]); + if (valueInspect.isAsciiOnly()) { + buffer.append(valueInspect.getByteList()); + } else { + String str = ByteList.decode(buffer.bytes(), buffer.getEncoding().getCharsetName()); + str += valueInspect; + buffer = new ByteList(str.getBytes(), valueInspect.getByteList().getEncoding()); + } } buffer.append('>'); diff --git a/spec/regression/GH-2182_struct_inspect_has_ascii_encoding_spec.rb b/spec/regression/GH-2182_struct_inspect_has_ascii_encoding_spec.rb new file mode 100644 index 000000000000..889ef0f26633 --- /dev/null +++ b/spec/regression/GH-2182_struct_inspect_has_ascii_encoding_spec.rb @@ -0,0 +1,20 @@ +# -*- encoding: utf-8 -*- + +# https://github.com/jruby/jruby/issues/2182 +if RUBY_VERSION > '1.9' + describe 'Struct#inspect' do + it 'returns correct value' do + s1 = Struct.new(:aa).new("ΆἅἇἈ") + s1.inspect.should == "#" + s1.inspect.encoding.should == Encoding::UTF_8 + + s2 = Struct.new(:a, :b).new("ΆἅἇἈ", "abc") + s2.inspect.should == "#" + s2.inspect.encoding.should == Encoding::UTF_8 + + s3 = Struct.new(:b).new("abc") + s3.inspect.should == "#" + s3.inspect.encoding.should == Encoding::ASCII_8BIT + end + end +end