Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-align Method#inspect logic with CRuby. Fixes #4995.
Browse files Browse the repository at this point in the history
headius committed Jan 22, 2018
1 parent 23783ce commit 288fc1b
Showing 3 changed files with 55 additions and 18 deletions.
63 changes: 45 additions & 18 deletions core/src/main/java/org/jruby/RubyMethod.java
Original file line number Diff line number Diff line change
@@ -217,35 +217,62 @@ public RubyUnboundMethod unbind() {
@JRubyMethod(name = {"inspect", "to_s"})
@Override
public IRubyObject inspect() {
StringBuilder buf = new StringBuilder("#<");
char delimeter = '#';

buf.append(getMetaClass().getRealClass().getName()).append(": ");
Ruby runtime = getRuntime();
ThreadContext context = runtime.getCurrentContext();

RubyString str = RubyString.newString(runtime, "#<");
String sharp = "#";

str.catString(getType().getName()).catString(": ");

RubyModule definedClass;
RubyModule mklass = method.getImplementationClass();

if (method instanceof AliasMethod) {
definedClass = method.getRealMethod().getImplementationClass();
}
else {
definedClass = method.getDefinedClass();
}

if (definedClass.isIncluded()) {
definedClass = definedClass.getMetaClass();
}

if (implementationModule.isSingleton()) {
IRubyObject attached = ((MetaClass) implementationModule).getAttached();
if (receiver == null) {
buf.append(implementationModule.inspect().toString());
str.cat19(inspect(context, implementationModule).convertToString());
} else if (receiver == attached) {
buf.append(attached.inspect().toString());
delimeter = '.';
str.cat19(inspect(context, attached).convertToString());
sharp = ".";
} else {
buf.append(receiver.inspect().toString());
buf.append('(').append(attached.inspect().toString()).append(')');
delimeter = '.';
str.cat19(inspect(context, receiver).convertToString());
str.catString("(");
str.cat19(inspect(context, attached).convertToString());
str.catString(")");
sharp = ".";
}
} else {
buf.append(originModule.getName());

str.catString(originModule.getName());
if (implementationModule != originModule) {
buf.append('(').append(implementationModule.getName()).append(')');
str.catString("(");
str.catString(implementationModule.getName());
str.catString(")");
}
}

buf.append(delimeter).append(methodName).append('>');

RubyString str = getRuntime().newString(buf.toString());
str.setTaint(isTaint());
str.catString(sharp);
str.catString(this.methodName);
if (!methodName.equals(method.getName())) {
str.catString("(");
str.catString(method.getName());
str.catString(")");
}
if (method.isNotImplemented()) {
str.catString(" (not-implemented)");
}
str.catString(">");

return str;
}

5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
@@ -1215,6 +1215,11 @@ public final void addMethodAtBootTimeOnly(String name, DynamicMethod method) {
method.setImplementationClass(methodLocation);
}

// if method does not have a name already, set it
if (method.getName() == null) {
method.setName(name);
}

methodLocation.getMethodsForWrite().put(name, method);

getRuntime().addProfiledMethod(name, method);
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -1341,6 +1341,11 @@ public final int cat19(ByteList other, int codeRange) {
return ptr_cr_ret[0];
}

public final RubyString catString(String str) {
cat19(encodeBytelist(str, getEncoding()), CR_UNKNOWN);
return this;
}

public final RubyString cat(RubyString str) {
return cat(str.getByteList());
}

0 comments on commit 288fc1b

Please sign in to comment.