Skip to content

Commit

Permalink
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
@@ -595,38 +595,36 @@ public final boolean respondsTo(String name) {
final DynamicMethod respondTo = getMetaClass().searchMethod("respond_to?");
final DynamicMethod respondToMissing = getMetaClass().searchMethod("respond_to_missing?");

// fastest path; builtin respond_to? and respond_to_missing? so we just check isMethodBound
if ( respondTo.equals(runtime.getRespondToMethod()) &&
respondToMissing.equals(runtime.getRespondToMissingMethod()) ) {
// fastest path; builtin respond_to? which just does isMethodBound
return getMetaClass().isMethodBound(name, false);
}

final ThreadContext context = runtime.getCurrentContext();
final RubySymbol mname = runtime.newSymbol(name);
final boolean respondToUndefined = respondTo.isUndefined();
if ( ! ( respondToUndefined && respondToMissing.isUndefined() ) ) {
// medium path, invoke user's respond_to?/respond_to_missing? if defined
final DynamicMethod method; final String respondName;
if ( respondToUndefined ) {
method = respondToMissing; respondName = "respond_to_missing?";
} else {
method = respondTo; respondName = "respond_to?";

// respond_to? or respond_to_missing? is not defined, so we must dispatch to trigger method_missing
if ( respondToUndefined ) {
return callMethod(context, "respond_to?", mname).isTrue();
}

// respond_to? is defined, invoke already-retrieved method object
final String respondName = "respond_to?";

// We have to check and enforce arity
final Arity arity = respondTo.getArity();
if ( arity.isFixed() ) {
if ( arity.required() == 1 ) {
return respondTo.call(context, this, metaClass, respondName, mname).isTrue();
}
// We have to check and enforce arity
final Arity arity = method.getArity();
if ( arity.isFixed() ) {
if ( arity.required() == 1 ) {
return method.call(context, this, metaClass, respondName, mname).isTrue();
}
if ( arity.required() != 2 ) {
throw runtime.newArgumentError(respondName + " must accept 1 or 2 arguments (requires " + arity.getValue() + ")");
}
if ( arity.required() != 2 ) {
throw runtime.newArgumentError(respondName + " must accept 1 or 2 arguments (requires " + arity.getValue() + ")");
}
return method.call(context, this, metaClass, respondName, mname, runtime.getTrue()).isTrue();
}

// slowest path, full callMethod to hit method_missing if present, or produce error
return callMethod(context, "respond_to?", mname).isTrue();
return respondTo.call(context, this, metaClass, respondName, mname, runtime.getTrue()).isTrue();
}

/**

0 comments on commit b4aa860

Please sign in to comment.