Skip to content

Commit b85fa82

Browse files
committedJan 24, 2018
Merge branch 'fix_method_inspect_4995' into jruby-9.1
2 parents eae124d + 90d7912 commit b85fa82

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed
 

‎core/src/main/java/org/jruby/RubyMethod.java

+45-18
Original file line numberDiff line numberDiff line change
@@ -217,35 +217,62 @@ public RubyUnboundMethod unbind() {
217217
@JRubyMethod(name = {"inspect", "to_s"})
218218
@Override
219219
public IRubyObject inspect() {
220-
StringBuilder buf = new StringBuilder("#<");
221-
char delimeter = '#';
222-
223-
buf.append(getMetaClass().getRealClass().getName()).append(": ");
220+
Ruby runtime = getRuntime();
221+
ThreadContext context = runtime.getCurrentContext();
222+
223+
RubyString str = RubyString.newString(runtime, "#<");
224+
String sharp = "#";
224225

226+
str.catString(getType().getName()).catString(": ");
227+
228+
RubyModule definedClass;
229+
RubyModule mklass = method.getImplementationClass();
230+
231+
if (method instanceof AliasMethod) {
232+
definedClass = method.getRealMethod().getImplementationClass();
233+
}
234+
else {
235+
definedClass = method.getDefinedClass();
236+
}
237+
238+
if (definedClass.isIncluded()) {
239+
definedClass = definedClass.getMetaClass();
240+
}
241+
225242
if (implementationModule.isSingleton()) {
226243
IRubyObject attached = ((MetaClass) implementationModule).getAttached();
227244
if (receiver == null) {
228-
buf.append(implementationModule.inspect().toString());
245+
str.cat19(inspect(context, implementationModule).convertToString());
229246
} else if (receiver == attached) {
230-
buf.append(attached.inspect().toString());
231-
delimeter = '.';
247+
str.cat19(inspect(context, attached).convertToString());
248+
sharp = ".";
232249
} else {
233-
buf.append(receiver.inspect().toString());
234-
buf.append('(').append(attached.inspect().toString()).append(')');
235-
delimeter = '.';
250+
str.cat19(inspect(context, receiver).convertToString());
251+
str.catString("(");
252+
str.cat19(inspect(context, attached).convertToString());
253+
str.catString(")");
254+
sharp = ".";
236255
}
237256
} else {
238-
buf.append(originModule.getName());
239-
257+
str.catString(originModule.getName());
240258
if (implementationModule != originModule) {
241-
buf.append('(').append(implementationModule.getName()).append(')');
259+
str.catString("(");
260+
str.catString(implementationModule.getName());
261+
str.catString(")");
242262
}
243263
}
244-
245-
buf.append(delimeter).append(methodName).append('>');
246-
247-
RubyString str = getRuntime().newString(buf.toString());
248-
str.setTaint(isTaint());
264+
str.catString(sharp);
265+
str.catString(this.methodName);
266+
if (!methodName.equals(method.getName())) {
267+
str.catString("(");
268+
str.catString(method.getName());
269+
str.catString(")");
270+
}
271+
if (method.isNotImplemented()) {
272+
str.catString(" (not-implemented)");
273+
}
274+
str.catString(">");
275+
249276
return str;
250277
}
251278

‎core/src/main/java/org/jruby/RubyModule.java

+5
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,11 @@ public final void addMethodAtBootTimeOnly(String name, DynamicMethod method) {
12151215
method.setImplementationClass(methodLocation);
12161216
}
12171217

1218+
// if method does not have a name already, set it
1219+
if (method.getName() == null) {
1220+
method.setName(name);
1221+
}
1222+
12181223
methodLocation.getMethodsForWrite().put(name, method);
12191224

12201225
getRuntime().addProfiledMethod(name, method);

‎core/src/main/java/org/jruby/RubyString.java

+5
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ public final int cat19(ByteList other, int codeRange) {
13411341
return ptr_cr_ret[0];
13421342
}
13431343

1344+
public final RubyString catString(String str) {
1345+
cat19(encodeBytelist(str, getEncoding()), CR_UNKNOWN);
1346+
return this;
1347+
}
1348+
13441349
public final RubyString cat(RubyString str) {
13451350
return cat(str.getByteList());
13461351
}

0 commit comments

Comments
 (0)
Please sign in to comment.