Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns a Matchdata object (or nil) based on self and the given pattern.
  • Computes regexp by converting pattern (if not already a Regexp).
    regexp = Regexp.new(pattern)
    
  • Computes matchdata, which will be either a MatchData object or nil (see Regexp#match):
    matchdata = <tt>regexp.match(self)
With no block given, returns the computed matchdata:
'foo'.match('f') # => #<MatchData "f">
'foo'.match('o') # => #<MatchData "o">
'foo'.match('x') # => nil
If Integer argument offset is given, the search begins at index offset:
'foo'.match('f', 1) # => nil
'foo'.match('o', 1) # => #<MatchData "o">
With a block given, calls the block with the computed matchdata and returns the block's return value:
'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o">
'foo'.match(/x/) {|matchdata| matchdata } # => nil
'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
 
               static VALUE
rb_str_match_m(int argc, VALUE *argv, VALUE str)
{
    VALUE re, result;
    if (argc < 1)
        rb_check_arity(argc, 1, 2);
    re = argv[0];
    argv[0] = str;
    result = rb_funcallv(get_pat(re), rb_intern("match"), argc, argv);
    if (!NIL_P(result) && rb_block_given_p()) {
        return rb_yield(result);
    }
    return result;
}
            

Was this page useful?