From ae613a37d98c14d045806293bfb74d9001848e25 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 12 Nov 2024 11:53:35 +0100 Subject: [PATCH] Move `Symbol#name` into `symbol.rb` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows to declare it as leaf just like `Symbol#to_s`. Co-Authored-By: Étienne Barrié --- string.c | 1 - symbol.rb | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/string.c b/string.c index 93f94fdb4ac234..9f54289dae5627 100644 --- a/string.c +++ b/string.c @@ -12740,7 +12740,6 @@ Init_String(void) rb_define_method(rb_cSymbol, "==", sym_equal, 1); rb_define_method(rb_cSymbol, "===", sym_equal, 1); rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0); - rb_define_method(rb_cSymbol, "name", rb_sym2str, 0); /* in symbol.c */ rb_define_method(rb_cSymbol, "to_proc", rb_sym_to_proc, 0); /* in proc.c */ rb_define_method(rb_cSymbol, "succ", sym_succ, 0); rb_define_method(rb_cSymbol, "next", sym_succ, 0); diff --git a/symbol.rb b/symbol.rb index bfac11ae21b21d..458d02b177e8f3 100644 --- a/symbol.rb +++ b/symbol.rb @@ -14,6 +14,20 @@ def to_s alias id2name to_s + # call-seq: + # name -> string + # + # Returns a frozen string representation of +self+ (not including the leading colon): + # + # :foo.name # => "foo" + # :foo.name.frozen? # => true + # + # Related: Symbol#to_s, Symbol#inspect. + def name + Primitive.attr! :leaf + Primitive.cexpr! 'rb_sym2str(self)' + end + # call-seq: # to_sym -> self #