From e5e4db1748d09635c6b20c3b880e5b55d84f7dea Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 12 Dec 2024 19:47:34 +0900 Subject: [PATCH] [Feature #20884] Define toplevel "Ruby" module with constants --- test/ruby/test_rubyoptions.rb | 13 +------------ variable.c | 3 --- version.c | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index ac4b5870eb..931d0cf7aa 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -1328,17 +1328,6 @@ class TestRubyOptions < Test::Unit::TestCase end def test_toplevel_ruby - reserved = ["", [], /::Ruby is reserved/] - env = {"RUBYOPT"=>""} - args = %w[-e Ruby=1] - assert_in_out_err([env, *args]) - assert_in_out_err([env, "-w", *args], *reserved) - assert_in_out_err([env, "-W:deprecated", *args], *reserved) - assert_in_out_err([env, "-w", "-W:no-deprecated", *args]) - - args = ["-e", "class A; Ruby=1; end"] - assert_in_out_err([env, *args]) - assert_in_out_err([env, "-w", *args]) - assert_in_out_err([env, "-W:deprecated", *args]) + assert_instance_of Module, ::Ruby end end diff --git a/variable.c b/variable.c index b73d38f469..66b960cabe 100644 --- a/variable.c +++ b/variable.c @@ -3626,9 +3626,6 @@ const_set(VALUE klass, ID id, VALUE val) } } } - if (klass == rb_cObject && id == idRuby) { - rb_warn_reserved_name_at(3.5, "::Ruby"); - } } void diff --git a/version.c b/version.c index 02e1c0bcb0..7a28c4a38f 100644 --- a/version.c +++ b/version.c @@ -87,10 +87,29 @@ const char ruby_engine[] = "ruby"; // Might change after initialization const char *rb_dynamic_description = ruby_description; +static inline void +define_ruby_const(const char *name, VALUE value, VALUE mRuby) +{ + rb_define_global_const(name, value); + if (strncmp(name, "RUBY_", rb_strlen_lit("RUBY_")) == 0) { + rb_define_const(mRuby, name + rb_strlen_lit("RUBY_"), value); + } +} + +/* RDoc needs rb_define_global_const */ +#define rb_define_global_const(name, value) \ + define_ruby_const(name, value, mRuby) + /*! Defines platform-depended Ruby-level constants */ void Init_version(void) { + /* + * The Ruby module that contains portable information among + * implementations. + */ + VALUE mRuby = rb_define_module("Ruby"); + enum {ruby_patchlevel = RUBY_PATCHLEVEL}; VALUE version = MKSTR(version); VALUE ruby_engine_name = MKSTR(engine); @@ -201,6 +220,7 @@ define_ruby_description(const char *const jit_opt) append(ruby_description + ruby_description_opt_point); # undef append + VALUE mRuby = rb_path2class("Ruby"); VALUE description = rb_obj_freeze(rb_usascii_str_new_static(desc, n)); rb_dynamic_description = desc; @@ -223,7 +243,9 @@ Init_ruby_description(ruby_cmdline_options_t *opt) void ruby_set_yjit_description(void) { + VALUE mRuby = rb_path2class("Ruby"); rb_const_remove(rb_cObject, rb_intern("RUBY_DESCRIPTION")); + rb_const_remove(mRuby, rb_intern("DESCRIPTION")); define_ruby_description(YJIT_DESCRIPTION); }