Fix Namespace RubyGems isolation issue
Ensure independent RubyGems environments per namespace
This fix resolves an issue where the Namespace feature fails to properly isolate RubyGems state. In the current implementation, RubyGems gets loaded into the root namespace and is shared across all user namespaces. This causes gem versions loaded in one namespace to affect others, leading to Gem::LoadError problems.
The fix explicitly removes any inherited Gem constant during main namespace and newly created namespace initialisation, then loads a fresh RubyGems environment in each namespace. This allows each namespace to manage its own gem dependencies without conflicts between namespaces.
Fixes Bug #21324.
Couldn't these be extracted as a function?
The created namespace is copied from the root namespace, so we need to check if the Gem exists in the root namespace.
The way to do it is... hmm, accessing RCLASSEXT_CONST_TBL(RCLASS_EXT_PRIME(rb_cObject)) is the only way for now. It might be good to add a function to do that specifically.
Do we need to load RubyGems explicitly in new Namespace if present in root one?
I'm not familiar to RubyGems itself, but it seems better to clear RubyGems's internal status than to remove-and-require rubygems.
And I guess this code doesn't work well because the .rb files of rubygems are already included in $LOADED_FEATURES.
It's not a correct fix, see https://bugs.ruby-lang.org/issues/21324#note-6