symbol_lookup.patch

Matthew Kerwin, 03/07/2013 12:50 PM

Download (1.83 KB)

View differences:

include/ruby/intern.h
583 583
VALUE rb_lastline_get(void);
584 584
void rb_lastline_set(VALUE);
585 585
VALUE rb_sym_all_symbols(void);
586
VALUE rb_sym_lookup(int argc, VALUE *argv);
586 587
/* process.c */
587 588
void rb_last_status_set(int status, rb_pid_t pid);
588 589
VALUE rb_last_status_get(void);
parse.y
10413 10413
    return ary;
10414 10414
}
10415 10415

  
10416
/*
10417
 *  call-seq:
10418
 *     Symbol[str]    => Symbol or nil
10419
 *
10420
 *  Returns an extant symbol, which is the .to_sym of +str+, or
10421
 *  nil if no such symbol exists.
10422
 *
10423
 *     Symbol['Object']      #=> :Object
10424
 *     Symbol['none such']   #=> nil
10425
 */
10426

  
10427
VALUE
10428
rb_sym_lookup(int argc, VALUE *argv)
10429
{
10430
    st_data_t id;
10431
    VALUE str,tmp;
10432
    rb_scan_args(argc, argv, "1", &str);
10433
    if (!RB_TYPE_P(str, T_STRING)) {
10434
	tmp = rb_check_string_type(str);
10435
	if (NIL_P(tmp)) {
10436
	    tmp = rb_inspect(str);
10437
	    rb_raise(rb_eTypeError, "%s is not a string",
10438
		     RSTRING_PTR(tmp));
10439
	}
10440
        str = tmp;
10441
    }
10442
    if (st_lookup(global_symbols.sym_id, (st_data_t)str, &id)) {
10443
	return ID2SYM(id);
10444
    }
10445
    return Qnil;
10446
}
10447

  
10416 10448
int
10417 10449
rb_is_const_id(ID id)
10418 10450
{
string.c
8307 8307
    rb_undef_alloc_func(rb_cSymbol);
8308 8308
    rb_undef_method(CLASS_OF(rb_cSymbol), "new");
8309 8309
    rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */
8310
    rb_define_singleton_method(rb_cSymbol, "[]", rb_sym_lookup, -1); /* in parse.y */
8310 8311

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