Issue #7854 has been updated by Yui NARUSE.


    diff --git a/string.c b/string.c
    index 4e30cb3..1e26a25 100644
    --- a/string.c
    +++ b/string.c
    @@ -8231,6 +8231,27 @@ str_scrub_bang(int argc, VALUE *argv, VALUE str)
    /*
    *  call-seq:
    + *     Symbol.find(str)   -> symbol or nil
    + *
    + *  Return the related symbol if the symbol already exists.
    + *  Return nil if not.
    + */
    +
    +static VALUE
    +sym_find(VALUE dummy, VALUE sym)
    +{
    +    ID id = rb_check_id(&sym);
    +
    +    if (id) {
    +	return ID2SYM(id);
    +    }
    +    else {
    +	return Qnil;
    +    }
    +}
    +
    +/*
    + *  call-seq:
    *     sym == obj   -> true or false
    *
    *  Equality---If <i>sym</i> and <i>obj</i> are exactly the same
    @@ -8787,6 +8808,7 @@ Init_String(void)
    rb_undef_alloc_func(rb_cSymbol);
    rb_undef_method(CLASS_OF(rb_cSymbol), "new");
    rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
    +    rb_define_singleton_method(rb_cSymbol, "find", sym_find, 1);
    rb_define_method(rb_cSymbol, "==", sym_equal, 1);
    rb_define_method(rb_cSymbol, "===", sym_equal, 1);
    diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
    index 7f261b6..cebaf43 100644
    --- a/test/ruby/test_symbol.rb
    +++ b/test/ruby/test_symbol.rb
    @@ -1,4 +1,5 @@
    require 'test/unit'
    +require_relative 'envutil'
    class TestSymbol < Test::Unit::TestCase
    # [ruby-core:3573]
    @@ -206,4 +207,12 @@ class TestSymbol < Test::Unit::TestCase
    assert_equal(true, "foo#{Time.now.to_i}".to_sym.frozen?)
    assert_equal(true, :foo.to_sym.frozen?)
    end
    +
    +  def test_sym_find
    +    assert_separately(%w[--disable=gems], <<-"end;")
    +      assert_equal :intern, Symbol.find("intern")
    +      assert_equal :hoge, Symbol.find("hoge")
    +      assert_raise(TypeError){ Symbol.find(true) }
    +    end;
    +  end
    end

----------------------------------------
Feature #7854: New method Symbol[string]
https://bugs.ruby-lang.org/issues/7854#change-45472

* Author: Nathan Zook
* Status: Rejected
* Priority: Normal
* Assignee: 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.


---Files--------------------------------
symbol_lookup.patch (1.83 KB)
symbol_lookup2.patch (1.36 KB)
symbol_lookup3.patch (1.07 KB)
symbol_lookup3_warn.patch (1.16 KB)


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