1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

string.c: rb_to_symbol

* string.c (rb_to_symbol): new function to convert an object to a
  symbol.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-07-30 12:36:31 +00:00
parent e1b064c576
commit 6db9db48ad
2 changed files with 24 additions and 8 deletions

View file

@ -810,6 +810,7 @@ void rb_gc_free_dsymbol(VALUE);
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);

View file

@ -8713,25 +8713,40 @@ sym_encoding(VALUE sym)
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