Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#match?
(::Regexp | ::string pattern, ?::int offset) -> bool
match?(pattern, offset = 0) → true or false
string.c
View on GitHub ↗
static VALUE
rb_str_match_m_p(int argc, VALUE *argv, VALUE str)
{
VALUE re;
rb_check_arity(argc, 1, 2);
re = get_pat(argv[0]);
return rb_reg_match_p(re, str, argc > 1 ? NUM2LEN(argv[1]) : 0);
}
Returns whether a match is found for self and the given arguments; does not update Regexp Global Variables.
Computes regexp by converting pattern (if not already a Regexp):
Example
regexp = Regexp.new(pattern)
The search for regexp in self begins at the given character offset. Returns true if a match is found, false otherwise:
Example
'foo'.match?(/o/) # => true 'foo'.match?('o') # => true 'foo'.match?(/x/) # => false 'foo'.match?('f', 1) # => false 'foo'.match?('o', 1) # => true
Related: see Querying.