Feature #7854 » symbol_lookup2.patch
| string.c | ||
|---|---|---|
|
/*
|
||
|
* call-seq:
|
||
|
* Symbol[str] => Symbol or nil
|
||
|
*
|
||
|
* Returns an extant symbol, which is the .to_sym of +str+, or
|
||
|
* nil if no such symbol exists.
|
||
|
*
|
||
|
* Symbol['Object'] #=> :Object
|
||
|
* Symbol['none such'] #=> nil
|
||
|
*/
|
||
|
VALUE
|
||
|
sym_lookup(int argc, VALUE *argv)
|
||
|
{
|
||
|
st_data_t id;
|
||
|
VALUE str,tmp;
|
||
|
rb_scan_args(argc, argv, "1", &str);
|
||
|
if (!RB_TYPE_P(str, T_STRING)) {
|
||
|
tmp = rb_check_string_type(str);
|
||
|
if (NIL_P(tmp)) {
|
||
|
tmp = rb_inspect(str);
|
||
|
rb_raise(rb_eTypeError, "%s is not a string",
|
||
|
RSTRING_PTR(tmp));
|
||
|
}
|
||
|
str = tmp;
|
||
|
}
|
||
|
if (id = rb_check_id(&str)) {
|
||
|
return ID2SYM(id);
|
||
|
}
|
||
|
return Qnil;
|
||
|
}
|
||
|
/*
|
||
|
* call-seq:
|
||
|
* sym == obj -> true or false
|
||
|
*
|
||
|
* Equality---If <i>sym</i> and <i>obj</i> are exactly the same
|
||
| ... | ... | |
|
rb_undef_alloc_func(rb_cSymbol);
|
||
|
rb_undef_method(CLASS_OF(rb_cSymbol), "new");
|
||
|
rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
|
||
|
rb_define_singleton_method(rb_cSymbol, "[]", sym_lookup, -1);
|
||
|
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
|
||
|
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
|
||