Project

General

Profile

Feature #11346

New #intern_str or similarly-named method of Symbol to return a cached, frozen string (like "string".freeze)

Added by stevej (Steve Jorgensen) over 4 years ago. Updated over 4 years ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:69951]

Description

While I don't think it would be a good idea to change the behavior of the existing Symbol#to_s method as suggested in issue 11066, I think that it would be extremely useful to have some instance method of Symbol to perform that operation.

If such a method is added, it might be nice if String, and maybe even Object offered the same method so the operation could be performed with indifference to the target and so optimizations could be applied to Classes other than just Symbol (e.g. NilClass, TrueClass, FalseClass, and maybe for smallish values of Fixnum).

Perhaps, the new method name would be #intern_str, and if that is added to Object, the default implementation could be something like to_s.intern_str which would abide by the contract of always returning the same frozen string object (assuming #to_s does), even though it builds an ordinary string as an intermediate object, so is not optimized.


Related issues

Related to Ruby master - Feature #11066: symbol.to_s performance - caching and returning a frozen stringOpenActions

History

Updated by stevej (Steve Jorgensen) over 4 years ago

In the description, "(assuming #to_s does)" should read "(assuming #to_s returns equivalent strings)".

#2

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Related to Feature #11066: symbol.to_s performance - caching and returning a frozen string added
#3

Updated by ko1 (Koichi Sasada) over 4 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

Any opinion?

Also available in: Atom PDF