From 44133346923781aaa1022d9b881aeb4e58dd5940 Mon Sep 17 00:00:00 2001 From: matz Date: Sun, 4 Feb 2007 19:17:33 +0000 Subject: * parse.y (rb_compose_ivar2): function to create a new ivar2 symbol from a symbol and a class. back-ported from matzruby. * parse.y (rb_decompose_ivar2): reverse function of rb_compose_ivar2(). * marshal.c (w_symbol): support class local instance variables. * marshal.c (r_object0): ditto. * compile.c (defined_expr): ditto. * compile.c (iseq_compile_each): ditto. * insns.def: add two new instructions: getinstancevariable2 and setinstancevariable2. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@11630 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'string.c') diff --git a/string.c b/string.c index faacf9542..89e88544c 100644 --- a/string.c +++ b/string.c @@ -4690,6 +4690,9 @@ sym_inspect(VALUE sym) VALUE str, klass = Qundef; ID id = SYM2ID(sym); + if (rb_is_instance2_id(id)) { + id = rb_decompose_ivar2(id, &klass); + } sym = rb_id2str(id); str = rb_str_new(0, RSTRING_LEN(sym)+1); RSTRING_PTR(str)[0] = ':'; @@ -4723,6 +4726,9 @@ rb_sym_to_s(VALUE sym) { ID id = SYM2ID(sym); + if (rb_is_instance2_id(id)) { + id = rb_decompose_ivar2(id, 0); + } return str_new3(rb_cString, rb_id2str(id)); } @@ -4868,6 +4874,27 @@ rb_to_id(VALUE name) return id; } +static VALUE +sym_div(VALUE sym, VALUE klass) +{ + ID id = SYM2ID(sym); + + if (!rb_is_instance2_id(id)) { + rb_raise(rb_eArgError, "symbol %s should be local instance variable", + rb_id2name(id)); + } + switch (TYPE(klass)) { + case T_CLASS: + case T_MODULE: + break; + default: + rb_check_type(klass, T_CLASS); + break; + } + id = rb_compose_ivar2(id, klass); + return ID2SYM(id); +} + /* * A String object holds and manipulates an arbitrary sequence of * bytes, typically representing characters. String objects may be created @@ -5008,6 +5035,7 @@ Init_String(void) rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in parse.y */ rb_define_singleton_method(rb_cSymbol, "intern", rb_sym_s_intern, 1); + rb_define_method(rb_cSymbol, "/", sym_div, 1); rb_define_method(rb_cSymbol, "==", sym_equal, 1); rb_define_method(rb_cSymbol, "to_i", sym_to_i, 0); rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0); -- cgit