![](/style/images/good.png)
![](/style/images/bad.png)
Ruby adds `Symbol#name` to return frozen string corresponding to the symbol name
source link: https://blog.saeloun.com/2020/09/09/ruby-adds-name-method-to-symbol
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Ruby adds `Symbol#name` to return frozen string corresponding to the symbol name
Sep 9, 2020 , by Vamsi Pavan Mahesh 1 minute readRuby adds
instance method #name
to Symbol
.
This method returns an instance of String
with the same underlying value as the instance of Symbol
:
:test.name
=> "test"
The resultant string is frozen.
irb(main):002:0> :laptop.name
=> "laptop"
irb(main):003:0> :laptop.name.object_id
=> 180
irb(main):004:0> :laptop.name.object_id
=> 180
irb(main):005:0> :laptop.name.frozen?
=> true
In the above example,
we could observe that the same object_id
is being returned for different invocations of :laptop.name
.
It indicates that no new instances of String
are allocated after the first allocation of “laptop”.
How is it different from #to_s on Symbol?
When we call #to_s
on a symbol,
the resulting string is not frozen.
A new instance of String
will be allocated when #to_s
is again called on the same instance of Symbol
.
irb(main):003:0> :laptop.to_s
=> "laptop"
irb(main):004:0> :laptop.to_s.object_id
=> 200
irb(main):005:0> :laptop.to_s.object_id
=> 220
In the above example,
we could see that a different instance of String
is allocated whenever #to_s
is called on :laptop
.
Where can we use this?
If the piece of code which calls #to_s
on a symbol
lies in a hotspot of our application,
it will result in creating lots of instances of String
and they need to be eventually garbage collected, resulting in additional GC efforts.
Instead,
we can call #name
on a symbol to obtain a frozen instance of String
.
The resulting frozen string will be re-used on subsequent invocations of #name
on the same instance of Symbol
.
Hence, it won’t be an overhead to the Garbage Collector.
History
A proposal
was made to request a frozen instance of String
by calling #to_s
on Symbol
some time back.
The same was implemented as an experimental feature in ruby 2.7.
Unfortunately, it had to be reverted due to some backward compatibility issues.
Recommend
-
84
DumpsterFire - "Security Incidents In A Box!" A modular, menu-driven, cross-platform tool for building customized, time-delayed, distributed security events. Easily create custom event chains for Blue Team drills and sensor / alert mapping. Re...
-
43
:gem::gem: Feel free to have a look to my ebook
-
5
Periodically I take episodes from the RubyTapas archives and publish them for free. This episode from October 2012 is about symbol literals, and how you can use alternative quoting syntax to embed and...
-
12
Ruby 字符串 Frozen 和 unfreeze 的问题 Ruby 字符串 Forze...
-
4
How do I put the URL in the table and find a corresponding string in the table? advertisements I am trying to search through a URL for a match...
-
42
Conversation Copy link Contributor...
-
6
Copy link Contributor nnethercote
-
2
In this article, we will discuss different ways to find the maximum value of a column and return its corresponding rows in a Pandas PataFrame. Table of Contents Preparing dataset To q...
-
3
Freezolite: the magic gem for keeping Ruby literals safely frozenNovember 14, 2023
-
4
Performance Gains in Ruby 3.3- String.dup vs. String#+ Jan 23, 2024 •
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK