Skip to content

Commit

Permalink
Fix jrubyGH-2182 on jruby-1_7
Browse files Browse the repository at this point in the history
  • Loading branch information
k77ch7 committed Jan 14, 2015
1 parent e6150c6 commit 4bf84a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
26 changes: 17 additions & 9 deletions core/src/main/java/org/jruby/RubyStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import org.jcodings.specific.ASCIIEncoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings.ID;
Expand Down Expand Up @@ -544,23 +545,30 @@ public IRubyObject call(IRubyObject obj, boolean recur) {
*/
private IRubyObject inspectStruct(final ThreadContext context) {
RubyArray member = __member__();
ByteList buffer = new ByteList("#<struct ".getBytes());
RubyString buffer = RubyString.newString(getRuntime(), new ByteList("#<struct ".getBytes()));

if (is1_8() || getMetaClass().getRealClass().getBaseName() != null) {
buffer.append(getMetaClass().getRealClass().getRealClass().getName().getBytes());
buffer.append(' ');
String className = getMetaClass().getRealClass().getRealClass().getName();
buffer.append(RubyString.newString(getRuntime(), new ByteList(className.getBytes())));
buffer.cat(' ');
}

for (int i = 0,k=member.getLength(); i < k; i++) {
if (i > 0) buffer.append(',').append(' ');
if (i > 0) buffer.cat(',').cat(' ');
// 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());
if (is1_8()) {
buffer.cat(RubyString.objAsString(context, member.eltInternal(i)));
buffer.cat('=');
buffer.cat(inspect(context, values[i]));
} else {
buffer.cat19(RubyString.objAsString(context, member.eltInternal(i)));
buffer.cat('=');
buffer.cat19(inspect(context, values[i]));
}
}

buffer.append('>');
return getRuntime().newString(buffer); // OBJ_INFECT
buffer.cat('>');
return buffer.dup(); // OBJ_INFECT
}

private boolean is1_8() {
Expand Down
24 changes: 24 additions & 0 deletions spec/regression/GH-2182_struct_inspect_has_ascii_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- 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 == "#<struct aa=\"ΆἅἇἈ\">"
s1.inspect.encoding.should == Encoding::UTF_8

s2 = Struct.new(:a, :b).new("ΆἅἇἈ", "abc")
s2.inspect.should == "#<struct a=\"ΆἅἇἈ\", b=\"abc\">"
s2.inspect.encoding.should == Encoding::UTF_8

s3 = Struct.new(:b).new("abc")
s3.inspect.should == "#<struct b=\"abc\">"
s3.inspect.encoding.should == Encoding::ASCII_8BIT

s4 = Struct.new(:"ΆἅἇἈ").new("aa")
s4.inspect.should == "#<struct ΆἅἇἈ=\"aa\">"
s4.inspect.encoding.should == Encoding::UTF_8
end
end
end

0 comments on commit 4bf84a9

Please sign in to comment.