Commit 232c582b
232c582b159bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified
Description
respond_to? on a class includes inherited Class/Module builtins
Foo.respond_to?(:new) / :name / :instance_methods returned false: the
class-receiver path only consulted user-defined singleton methods and
module_function defs, not the methods every class object inherits from Class
and Module.
Add a builtin Class/Module method set (name, instance_methods, superclass,
ancestors, method_defined?, const_get, define_method, ...) to the class-
receiver resolution, with new answered true for a class and false for a
module.
Fixes #1408 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 232c582b159b
- Parent
- baf27ece4e13
src/codegen_call.cmodifiedView file
@@ -3481,6 +3481,22 @@
34813481
const char *dt = dn >= 0 ? nt_type(nt, dn) : NULL;34823482
if (dt && !strcmp(dt, "ModuleNode")) yes = comp_method_in_chain(c, ci, qm, NULL) >= 0;34833483
}3484+
/* builtin Class/Module methods every class object inherits */3485+
if (!yes) {3486+
static const char *const cls_uni[] = {3487+
"name", "instance_methods", "public_instance_methods",3488+
"private_instance_methods", "protected_instance_methods",3489+
"instance_method", "method_defined?", "superclass", "ancestors",3490+
"include?", "const_get", "const_set", "const_defined?",3491+
"define_method", "allocate", "<", "<=", ">", ">=", NULL };3492+
for (int u = 0; cls_uni[u]; u++) if (!strcmp(qm, cls_uni[u])) { yes = 1; break; }3493+
/* `new`: a class responds, a module does not */3494+
if (!yes && !strcmp(qm, "new")) {3495+
int dn = c->classes[ci].def_node;3496+
const char *dt = dn >= 0 ? nt_type(nt, dn) : NULL;3497+
yes = !(dt && !strcmp(dt, "ModuleNode"));3498+
}3499+
}34843500
}34853501
}34863502
else if (ty_is_object(rt)) {test/respond_to_class_builtins.rbaddedView file
Loading diff…