Define main.to_s even in namespaces

It just shows "main" just like the main object without namespace.
All main objects in namespaces will show "main" and it is impossible
to determine a main from main objects if it returns "main".
But it's not a problem because we don't define anything on main
objects usually.
This commit is contained in:
Satoshi Tagomori 2025-10-14 21:42:55 +09:00 committed by Satoshi Tagomori
parent 7e07a8d8f6
commit 9743b51806
Notes: git 2025-10-14 13:59:55 +00:00

View File

@ -118,6 +118,12 @@ namespace_generate_id(void)
return id;
}
static VALUE
namespace_main_to_s(VALUE obj)
{
return rb_str_new2("main");
}
static void
namespace_entry_initialize(rb_namespace_t *ns)
{
@ -128,9 +134,8 @@ namespace_entry_initialize(rb_namespace_t *ns)
ns->ns_id = 0;
ns->top_self = rb_obj_alloc(rb_cObject);
// TODO:
// rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0);
// rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
rb_define_singleton_method(ns->top_self, "to_s", namespace_main_to_s, 0);
rb_define_alias(rb_singleton_class(ns->top_self), "inspect", "to_s");
ns->load_path = rb_ary_dup(root->load_path);
ns->expanded_load_path = rb_ary_dup(root->expanded_load_path);
ns->load_path_snapshot = rb_ary_new();