Skip to content
Browse files

implement Symbol#match

  • Loading branch information...
1 parent 2cc16aa commit 64299cdbace21c7aa53e0ffb363e7e1c3f04af6c @Watson1978 Watson1978 committed Jun 23, 2012
Showing with 22 additions and 2 deletions.
  1. +1 −0 include/ruby/intern.h
  2. +0 −2 spec/frozen/tags/macruby/core/symbol/match_tags.txt
  3. +6 −0 string.c
  4. +15 −0 symbol.c
View
1 include/ruby/intern.h
@@ -561,6 +561,7 @@ void rb_str_setter(VALUE, ID, VALUE*);
VALUE rb_str_intern(VALUE);
VALUE rb_sym_to_s(VALUE);
VALUE rb_str_length(VALUE);
+VALUE rb_str_match(VALUE, VALUE);
// Return a string object appropriate for bstr_ calls. This does nothing for
// data/binary RubyStrings.
View
2 spec/frozen/tags/macruby/core/symbol/match_tags.txt
@@ -1,2 +0,0 @@
-fails:Symbol#match returns the index of the beginning of the match
-fails:Symbol#match sets the last match pseudo-variables
View
6 string.c
@@ -3168,6 +3168,12 @@ rstr_match(VALUE self, SEL sel, VALUE other)
}
}
+VALUE
+rb_str_match(VALUE self, VALUE other)
+{
+ return rstr_match(self, 0, other);
+}
+
/*
* call-seq:
* str.scan(pattern) => array
View
15 symbol.c
@@ -678,6 +678,19 @@ rsym_aref(VALUE sym, SEL sel, int argc, VALUE *argv)
/*
* call-seq:
+ * sym =~ obj -> fixnum or nil
+ *
+ * Returns <code>sym.to_s =~ obj</code>.
+ */
+
+static VALUE
+rsym_match(VALUE sym, SEL sel, VALUE other)
+{
+ return rb_str_match(rb_sym_to_s(sym), other);
+}
+
+/*
+ * call-seq:
* sym.upcase => symbol
*
* Same as <code>sym.to_s.upcase.intern</code>.
@@ -799,6 +812,8 @@ Init_Symbol(void)
rb_objc_define_method(rb_cSymbol, "to_sym", rsym_to_sym, 0);
rb_objc_define_method(rb_cSymbol, "empty?", rsym_empty, 0);
rb_objc_define_method(rb_cSymbol, "[]", rsym_aref, -1);
+ rb_objc_define_method(rb_cSymbol, "=~", rsym_match, 1);
+ rb_objc_define_method(rb_cSymbol, "match", rsym_match, 1);
rb_objc_define_method(rb_cSymbol, "upcase", rsym_upcase, 0);
rb_objc_define_method(rb_cSymbol, "downcase", rsym_downcase, 0);
rb_objc_define_method(rb_cSymbol, "swapcase", rsym_swapcase, 0);

0 comments on commit 64299cd

Please sign in to comment.
Something went wrong with that request. Please try again.