symbol_lookup2.patch

Matthew Kerwin, 03/08/2013 08:48 AM

Download (1.36 KB)

View differences:

string.c
7754 7754

  
7755 7755
/*
7756 7756
 *  call-seq:
7757
 *     Symbol[str]    => Symbol or nil
7758
 *
7759
 *  Returns an extant symbol, which is the .to_sym of +str+, or
7760
 *  nil if no such symbol exists.
7761
 *
7762
 *     Symbol['Object']      #=> :Object
7763
 *     Symbol['none such']   #=> nil
7764
 */
7765

  
7766
VALUE
7767
sym_lookup(int argc, VALUE *argv)
7768
{
7769
    st_data_t id;
7770
    VALUE str,tmp;
7771
    rb_scan_args(argc, argv, "1", &str);
7772
    if (!RB_TYPE_P(str, T_STRING)) {
7773
	tmp = rb_check_string_type(str);
7774
	if (NIL_P(tmp)) {
7775
	    tmp = rb_inspect(str);
7776
	    rb_raise(rb_eTypeError, "%s is not a string",
7777
		     RSTRING_PTR(tmp));
7778
	}
7779
        str = tmp;
7780
    }
7781
    if (id = rb_check_id(&str)) {
7782
	return ID2SYM(id);
7783
    }
7784
    return Qnil;
7785
}
7786

  
7787

  
7788
/*
7789
 *  call-seq:
7757 7790
 *     sym == obj   -> true or false
7758 7791
 *
7759 7792
 *  Equality---If <i>sym</i> and <i>obj</i> are exactly the same
......
8307 8340
    rb_undef_alloc_func(rb_cSymbol);
8308 8341
    rb_undef_method(CLASS_OF(rb_cSymbol), "new");
8309 8342
    rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
8343
    rb_define_singleton_method(rb_cSymbol, "[]", sym_lookup, -1);
8310 8344

  
8311 8345
    rb_define_method(rb_cSymbol, "==", sym_equal, 1);
8312 8346
    rb_define_method(rb_cSymbol, "===", sym_equal, 1);