VOOZH about

URL: https://apidock.com/ruby/String/intern

⇱ String#intern - APIdock


method

intern

ruby latest stable - Class: String
intern()
public

Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

"Koala".intern #=> :Koala
s = 'cat'.to_sym #=> :cat
s == :cat #=> true
s = '@cat'.to_sym #=> :@cat
s == :@cat #=> true

This can also be used to create symbols that cannot be represented using the :xxx notation.

'cat and dog'.to_sym #=> :"cat and dog"
VALUE
rb_str_intern(VALUE str)
{
#if USE_SYMBOL_GC
 rb_encoding *enc, *ascii;
 int type;
#else
 ID id;
#endif
 VALUE sym = lookup_str_sym(str);

 if (sym) {
	return sym;
 }

#if USE_SYMBOL_GC
 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);
#else
 id = intern_str(str, 0);
 return ID2SYM(id);
#endif
}

Related methods