mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[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]
This commit is contained in:
parent
c8f7168653
commit
9d0866c7d7
Notes:
git
2019-09-26 20:25:58 +09:00
3 changed files with 24 additions and 9 deletions
4
NEWS
4
NEWS
|
@ -174,6 +174,10 @@ Module::
|
||||||
|
|
||||||
* Module#autoload? now takes an +inherit+ optional argument, like as
|
* Module#autoload? now takes an +inherit+ optional argument, like as
|
||||||
Module#const_defined?. [Feature #15777]
|
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::
|
ObjectSpace::WeakMap::
|
||||||
|
|
||||||
|
|
|
@ -102,13 +102,27 @@ describe "Module#name" do
|
||||||
m::N.name.should == "ModuleSpecs::Anonymous::E::N"
|
m::N.name.should == "ModuleSpecs::Anonymous::E::N"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a mutable string" do
|
ruby_version_is ""..."2.7" do
|
||||||
ModuleSpecs.name.frozen?.should be_false
|
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
|
end
|
||||||
|
|
||||||
it "returns a mutable string that when mutated does not modify the original module name" do
|
ruby_version_is "2.7" do
|
||||||
ModuleSpecs.name << "foo"
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -107,10 +107,7 @@ VALUE
|
||||||
rb_mod_name(VALUE mod)
|
rb_mod_name(VALUE mod)
|
||||||
{
|
{
|
||||||
int permanent;
|
int permanent;
|
||||||
VALUE path = classname(mod, &permanent);
|
return classname(mod, &permanent);
|
||||||
|
|
||||||
if (!NIL_P(path)) return rb_str_dup(path);
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue