summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSatoshi Tagomori <s-tagomori@sakura.ad.jp>2025-10-14 21:42:55 +0900
committerSatoshi Tagomori <tagomoris@gmail.com>2025-10-14 22:59:14 +0900
commit9743b51806a8dc6843444e42f8e838831da99bcd (patch)
tree1488406ced82afe2aa271320fe550d8f30f9c320
parent7e07a8d8f662edcf112094750f6e2abed8b84803 (diff)
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.
-rw-r--r--namespace.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/namespace.c b/namespace.c
index bf4f87f1de..c8c947f50c 100644
--- a/namespace.c
+++ b/namespace.c
@@ -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();