ruby-changes:57927
From: Jean <ko1@a...>
Date: Thu, 26 Sep 2019 20:25:45 +0900 (JST)
Subject: [ruby-changes:57927] 9d0866c7d7 (master): [EXPERIMENTAL] Make Module#name return a frozen String
https://git.ruby-lang.org/ruby.git/commit/?id=9d0866c7d7 From 9d0866c7d7b9cbe36a851744a37806e747e0e7a8 Mon Sep 17 00:00:00 2001 From: Jean Boussier <jean.boussier@g...> Date: Thu, 26 Sep 2019 10:41:43 +0200 Subject: [EXPERIMENTAL] Make Module#name return a frozen String * Always the same frozen String for a given Module or Class. * Avoids extra allocations whenever calling Module#name. * See [Feature #16150] diff --git a/NEWS b/NEWS index 4e16851..d23cadd 100644 --- a/NEWS +++ b/NEWS @@ -174,6 +174,10 @@ Module:: https://github.com/ruby/ruby/blob/trunk/NEWS#L174 * Module#autoload? now takes an +inherit+ optional argument, like as Module#const_defined?. [Feature #15777] + + * Module#name now always return a frozen String. The returned String is + always the same for a given Module. This change is experimental + [Feature #16150] ObjectSpace::WeakMap:: diff --git a/spec/ruby/core/module/name_spec.rb b/spec/ruby/core/module/name_spec.rb index d646843..36a91f5 100644 --- a/spec/ruby/core/module/name_spec.rb +++ b/spec/ruby/core/module/name_spec.rb @@ -102,13 +102,27 @@ describe "Module#name" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/name_spec.rb#L102 m::N.name.should == "ModuleSpecs::Anonymous::E::N" end - it "returns a mutable string" do - ModuleSpecs.name.frozen?.should be_false + ruby_version_is ""..."2.7" do + it "returns a mutable string" do + ModuleSpecs.name.frozen?.should be_false + end + + it "returns a mutable string that when mutated does not modify the original module name" do + ModuleSpecs.name << "foo" + + ModuleSpecs.name.should == "ModuleSpecs" + end end - it "returns a mutable string that when mutated does not modify the original module name" do - ModuleSpecs.name << "foo" + ruby_version_is "2.7" do + it "returns a frozen String" do + ModuleSpecs.name.frozen?.should == true + end - ModuleSpecs.name.should == "ModuleSpecs" + it "always returns the same String for a given Module" do + s1 = ModuleSpecs.name + s2 = ModuleSpecs.name + s1.should equal(s2) + end end end diff --git a/variable.c b/variable.c index 9fd075c..aafb6f4 100644 --- a/variable.c +++ b/variable.c @@ -107,10 +107,7 @@ VALUE https://github.com/ruby/ruby/blob/trunk/variable.c#L107 rb_mod_name(VALUE mod) { int permanent; - VALUE path = classname(mod, &permanent); - - if (!NIL_P(path)) return rb_str_dup(path); - return path; + return classname(mod, &permanent); } static VALUE -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/