Issue #7854 has been updated by phluid61 (Matthew Kerwin).


=begin
In the intervening months I've created a gem ((<URL:https://rubygems.org/gems/symbol_lookup>)) that implements ((%Symbol.[]%)), as well as two methods inspired by #7795 :
* String#interned => gets an existing symbol, returning the symbol or nil
* String#to_existing_sym => gets an existing symbol, raising an argument error if it doesn't exist

The problem is that they are written as C extensions, and I'm not familiar enough with non-MRI implementations to port (or create a multi-platform version of) the gem; so the uptake is relatively limited.  If it was promoted to core the functionality would become available to everyone, and I'm certain it would be used, for example the Rails team could use it to build an alternate solution to the problem addressed in #7839.
=end
----------------------------------------
Feature #7854: New method Symbol[string]
https://bugs.ruby-lang.org/issues/7854#change-40196

Author: Student (Nathan Zook)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


I propose a new class method [] on Symbol.  If a symbol s already exists such that s.to_s == string, then s is returned.  If not, nil is returned.

The inspiration for this method is a question I was asked, and the answer I was given:  "Why would you want to turn a tainted string into a symbol?"  "I don't--I want to access an existing symbol with tainted data".  Symbol[] accesses the symbol table like hash[] accesses the elements of a hash.

I believe that this completely addresses the problems behind tickets #7791 and #7839.  I believe that it is a more intuitive solution than my proposal #7795, and I believe that this will also be useful for YAML.safe_load and similar initiatives.



-- 
http://bugs.ruby-lang.org/