ruby-changes:34920
From: nobu <ko1@a...>
Date: Wed, 30 Jul 2014 21:36:40 +0900 (JST)
Subject: [ruby-changes:34920] nobu:r47003 (trunk): string.c: rb_to_symbol
nobu 2014-07-30 21:36:31 +0900 (Wed, 30 Jul 2014) New Revision: 47003 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47003 Log: string.c: rb_to_symbol * string.c (rb_to_symbol): new function to convert an object to a symbol. Modified files: trunk/internal.h trunk/string.c Index: string.c =================================================================== --- string.c (revision 47002) +++ string.c (revision 47003) @@ -8713,25 +8713,40 @@ sym_encoding(VALUE sym) https://github.com/ruby/ruby/blob/trunk/string.c#L8713 return rb_obj_encoding(rb_sym2str(sym)); } -ID -rb_to_id(VALUE name) +static VALUE +string_for_symbol(VALUE name) { - VALUE tmp; - - if (SYMBOL_P(name)) { - return SYM2ID(name); - } if (!RB_TYPE_P(name, T_STRING)) { - tmp = rb_check_string_type(name); + VALUE tmp = rb_check_string_type(name); if (NIL_P(tmp)) { rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol", name); } name = tmp; } + return name; +} + +ID +rb_to_id(VALUE name) +{ + if (SYMBOL_P(name)) { + return SYM2ID(name); + } + name = string_for_symbol(name); return rb_intern_str(name); } +VALUE +rb_to_symbol(VALUE name) +{ + if (SYMBOL_P(name)) { + return name; + } + name = string_for_symbol(name); + return rb_str_dynamic_intern(name); +} + /* * A <code>String</code> object holds and manipulates an arbitrary sequence of * bytes, typically representing characters. String objects may be created Index: internal.h =================================================================== --- internal.h (revision 47002) +++ internal.h (revision 47003) @@ -810,6 +810,7 @@ void rb_gc_free_dsymbol(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L810 VALUE rb_str_dynamic_intern(VALUE); ID rb_id_attrget(ID id); +VALUE rb_to_symbol(VALUE name); VALUE rb_check_symbol(volatile VALUE *namep); #ifdef RUBY_ENCODING_H VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/