to_sym()
public
Returns the Symbol corresponding to
str, creating the symbol if it did not previously exist. See Symbol#id2name.
"Koala".intern
s = 'cat'.to_sym
s == :cat
s = '@cat'.to_sym
s == :@cat
This can also be used to create symbols that cannot be represented using
the :xxx notation.
'cat and dog'.to_sym
Show source
VALUE
rb_str_intern(VALUE str)
{
rb_encoding *enc, *ascii;
int type;
ID id;
VALUE sym = lookup_str_sym(str);
if (sym) {
return sym;
}
enc = rb_enc_get(str);
ascii = rb_usascii_encoding();
if (enc != ascii && sym_check_asciionly(str)) {
str = rb_str_dup(str);
rb_enc_associate(str, ascii);
OBJ_FREEZE(str);
enc = ascii;
}
else {
str = rb_str_new_frozen(str);
}
str = rb_fstring(str);
type = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
if (type < 0) type = ID_JUNK;
return dsymbol_alloc(rb_cSymbol, str, enc, type);
id = intern_str(str, 0);
return ID2SYM(id);
}