symbol.c: add `Symbol#name` method
`Symbol#name` is similar to `#to_s` but returns a frozen string.
MODIFY
src/symbol.c
src/symbol.c
@@ -401,6 +401,30 @@ sym_to_s(mrb_state *mrb, mrb_value sym)
return mrb_sym_str(mrb, mrb_symbol(sym));
}
+/*
+ * call-seq:
+ * sym.name -> string
+ *
+ * Returns the name or string corresponding to <i>sym</i>. Unlike #to_s, the
+ * returned string is frozen.
+ *
+ * :fred.name #=> "fred"
+ * :fred.name.frozen? #=> true
+ */
+static mrb_value
+sym_name(mrb_state *mrb, mrb_value vsym)
+{
+ mrb_sym sym = mrb_symbol(vsym);
+ mrb_int len;
+ const char *name = mrb_sym_name_len(mrb, sym, &len);
+
+ mrb_assert(name != NULL);
+ if (SYMBOL_INLINE_P(sym)) {
+ return mrb_str_new_frozen(mrb, name, len);
+ }
+ return mrb_str_new_static_frozen(mrb, name, len);
+}
+
/* 15.2.11.3.4 */
/*
* call-seq:
@@ -657,6 +681,7 @@ mrb_init_symbol(mrb_state *mrb)
mrb_undef_class_method(mrb, sym, "new");
mrb_define_method(mrb, sym, "to_s", sym_to_s, MRB_ARGS_NONE()); /* 15.2.11.3.3 */
+ mrb_define_method(mrb, sym, "name", sym_name, MRB_ARGS_NONE());
mrb_define_method(mrb, sym, "to_sym", sym_to_sym, MRB_ARGS_NONE()); /* 15.2.11.3.4 */
mrb_define_method(mrb, sym, "inspect", sym_inspect, MRB_ARGS_NONE()); /* 15.2.11.3.5(x) */
mrb_define_method(mrb, sym, "<=>", sym_cmp, MRB_ARGS_REQ(1));

symbol.c: add `Symbol#name` method

`Symbol#name` is similar to `#to_s` but returns a frozen string.