[Feature #20884] Define toplevel "Ruby" module with constants

This commit is contained in:
Nobuyoshi Nakada 2024-12-12 19:47:34 +09:00 committed by Nobuyoshi Nakada
parent 7f738bb5d7
commit e5e4db1748
Notes: git 2024-12-25 10:12:53 +00:00
3 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
}