[前][次][番号順一覧][スレッド一覧]

ruby-changes:62829

From: Nobuyoshi <ko1@a...>
Date: Fri, 4 Sep 2020 22:19:53 +0900 (JST)
Subject: [ruby-changes:62829] eb67c603ca (master): Added Symbol#name

https://git.ruby-lang.org/ruby.git/commit/?id=eb67c603ca

From eb67c603ca7e435181684857e650b4633fda5bb6 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 4 Sep 2020 22:18:59 +0900
Subject: Added Symbol#name

https://bugs.ruby-lang.org/issues/16150#change-87446

diff --git a/NEWS.md b/NEWS.md
index 1bc19e8..4b4d13a 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -165,6 +165,12 @@ Outstanding ones only. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L165
         * Symbol#to_proc now returns a lambda Proc.
           [[Feature #16260]]
 
+    * New method
+
+        * Symbol#name, which returns the name of the symbol if it is
+          named.  The returned string cannot be modified.
+          [[Feature #16150]]
+
 * Warning
 
     * Modified method
@@ -343,6 +349,7 @@ Excluding feature bug fixes. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L349
 [Feature #16378]: https://bugs.ruby-lang.org/issues/16378
 [Feature #16828]: https://bugs.ruby-lang.org/issues/16828
 [Bug #14541]:     https://bugs.ruby-lang.org/issues/14541
+[Feature #16150]: https://bugs.ruby-lang.org/issues/16150
 [Feature #16175]: https://bugs.ruby-lang.org/issues/16175
 [Feature #15973]: https://bugs.ruby-lang.org/issues/15973
 [Feature #16614]: https://bugs.ruby-lang.org/issues/16614
diff --git a/string.c b/string.c
index b9cc048..7cfeaae 100644
--- a/string.c
+++ b/string.c
@@ -11539,6 +11539,7 @@ Init_String(void) https://github.com/ruby/ruby/blob/trunk/string.c#L11539
     rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
     rb_define_method(rb_cSymbol, "to_s", rb_sym_to_s, 0);
     rb_define_method(rb_cSymbol, "id2name", rb_sym_to_s, 0);
+    rb_define_method(rb_cSymbol, "name", rb_sym2str, 0);
     rb_define_method(rb_cSymbol, "intern", sym_to_sym, 0);
     rb_define_method(rb_cSymbol, "to_sym", sym_to_sym, 0);
     rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0);
diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
index f6ef70f..f7f17b8 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -105,6 +105,12 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L105
     end
   end
 
+  def test_name
+    assert_equal("foo", :foo.name)
+    assert_same(:foo.name, :foo.name)
+    assert_predicate(:foo.name, :frozen?)
+  end
+
   def test_to_proc
     assert_equal %w(1 2 3), (1..3).map(&:to_s)
     [
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]