diff --git a/ChangeLog b/ChangeLog index ac76eaeacf..46a5cb8232 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Wed Feb 14 13:12:06 2007 Yukihiro Matsumoto + + * re.c (reg_operand): allow symbols to be operands for regular + expression matches. + + * string.c (Init_String): allow Symbol#===. + + * lib/date/format.rb (Date::Format::Bag::to_hash): string + added prefixes. + Wed Feb 14 12:58:38 2007 Koichi Sasada * thread.c (do_select): fix to iterate select(). diff --git a/lib/date/format.rb b/lib/date/format.rb index 152ca2ff6b..0a4869e3a1 100644 --- a/lib/date/format.rb +++ b/lib/date/format.rb @@ -111,8 +111,8 @@ class Date def to_hash instance_variables. - select{|n| !instance_variable_get(n).nil?}.grep(/\A@[^_]/). - inject({}){|r, n| r[n[1..-1].intern] = instance_variable_get(n); r} + select{|n| !instance_variable_get(n).nil?}.grep(/\A@v[^_]/). + inject({}){|r, n| r[n[2..-1].intern] = instance_variable_get(n); r} end end diff --git a/re.c b/re.c index 1a01490e5b..b06e229fe9 100644 --- a/re.c +++ b/re.c @@ -1585,6 +1585,22 @@ rb_reg_equal(VALUE re1, VALUE re2) return Qfalse; } +static VALUE +reg_operand(VALUE s, int check) +{ + if (SYMBOL_P(s)) { + return rb_sym_to_s(s); + } + else { + VALUE tmp = rb_check_string_type(s); + if (check && NIL_P(tmp)) { + rb_raise(rb_eTypeError, "can't convert %s to String", + rb_obj_classname(s)); + } + return tmp; + } +} + static VALUE rb_reg_match_pos(VALUE re, VALUE str, long pos) { @@ -1592,7 +1608,7 @@ rb_reg_match_pos(VALUE re, VALUE str, long pos) rb_backref_set(Qnil); return Qnil; } - StringValue(str); + str = reg_operand(str, Qtrue); if (pos != 0) { if (pos < 0) { pos += RSTRING_LEN(str); @@ -1647,14 +1663,11 @@ rb_reg_eqq(VALUE re, VALUE str) { long start; - if (TYPE(str) != T_STRING) { - str = rb_check_string_type(str); - if (NIL_P(str)) { - rb_backref_set(Qnil); - return Qfalse; - } + str = reg_operand(str, Qfalse); + if (NIL_P(str)) { + rb_backref_set(Qnil); + return Qfalse; } - StringValue(str); start = rb_reg_search(re, str, 0, 0); if (start < 0) { return Qfalse; @@ -1912,7 +1925,7 @@ rb_reg_s_quote(int argc, VALUE *argv) curr_kcode = reg_kcode; reg_kcode = kcode_saved; } - StringValue(str); + str = reg_operand(str, Qtrue); str = rb_reg_quote(str); kcode_reset_option(); return str; diff --git a/string.c b/string.c index 89e88544c9..17e3927812 100644 --- a/string.c +++ b/string.c @@ -4809,6 +4809,13 @@ sym_match(VALUE sym, VALUE other) return rb_str_match(rb_sym_to_s(sym), other); } +static VALUE +sym_eqq(VALUE sym, VALUE other) +{ + if (sym == other) return Qtrue; + return rb_str_equal(rb_sym_to_s(sym), other); +} + static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { @@ -5051,6 +5058,7 @@ Init_String(void) rb_define_method(rb_cSymbol, "<=>", sym_cmp, 1); rb_define_method(rb_cSymbol, "casecmp", sym_casecmp, 1); rb_define_method(rb_cSymbol, "=~", sym_match, 1); + rb_define_method(rb_cSymbol, "===", sym_eqq, 1); rb_define_method(rb_cSymbol, "[]", sym_aref, -1); rb_define_method(rb_cSymbol, "slice", sym_aref, -1);