Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mrbgems/mruby-regexp/mrblib/string_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def match?(re, pos = 0)
end

def =~(re)
# A String argument would dispatch back to this method and recurse, so
# reject it up front (CRuby raises the same TypeError).
raise TypeError, "type mismatch: String given" if re.is_a?(String)
re =~ self
end

Expand Down
10 changes: 10 additions & 0 deletions mrbgems/mruby-regexp/test/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@
assert_equal "world", md[2]
end

assert("String#=~") do
assert_equal 1, "abc" =~ Regexp.new("b")
assert_nil "abc" =~ Regexp.new("z")
end

assert("String#=~ with a String argument raises TypeError") do
assert_raise(TypeError) { "abc" =~ "b" }
assert_raise(TypeError) { "abc" !~ "b" }
end

assert("String#sub") do
assert_equal "hXllo", "hello".sub(Regexp.new("e"), "X")
end
Expand Down