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 6 years ago. Updated about 1 month ago.

Target version:


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 stringRejectedActions

Updated by stevej (Steve Jorgensen) over 6 years ago

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

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

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

Updated by ko1 (Koichi Sasada) over 6 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

Any opinion?

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

  • Status changed from Open to Closed

Symbol#name was added in Ruby 3.0 for this.


Also available in: Atom PDF