summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-30 12:36:31 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-30 12:36:31 +0000
commit6db9db48aded195bda3a888ceac0c1cdd7c62d05 (patch)
tree4652e2c851dbff848d405466f79948975b3ff474
parente1b064c576007805067359479c8595a2f3cc23ef (diff)
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
-rw-r--r--internal.h1
-rw-r--r--string.c31
2 files changed, 24 insertions, 8 deletions
diff --git a/internal.h b/internal.h
index c91670a3882..4a5134d20aa 100644
--- a/internal.h
+++ b/internal.h
@@ -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);
diff --git a/string.c b/string.c
index 9f06837789e..bec0bfd839d 100644
--- a/string.c
+++ b/string.c
@@ -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