Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String gsub! #1768

Merged
merged 3 commits into from Mar 22, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/string.rbs
Expand Up @@ -2098,7 +2098,7 @@ class String
# Related: String#sub, String#gsub, String#sub!.
#
def gsub!: (Regexp | string pattern, string | hash[String, _ToS] replacement) -> self?
| (Regexp | string pattern) -> Enumerator[String, self]
| (Regexp | string pattern) -> Enumerator[String, self?]
| (Regexp | string pattern) { (String match) -> _ToS } -> self?

# <!--
Expand Down
17 changes: 10 additions & 7 deletions lib/rbs/test/type_check.rb
Expand Up @@ -283,19 +283,22 @@ def value(val, type)
end
end

each_sample(values).all? do |v|
value_check = values.empty? || each_sample(values).all? do |v|
if v.size == 1
# Only one block argument.
value(v[0], type.args[0]) || value(v, type.args[0])
else
value(v, type.args[0])
end
end &&
if ret.equal?(self)
type.args[1].is_a?(Types::Bases::Bottom)
else
value(ret, type.args[1])
end
end

return_check = if ret.equal?(self)
type.args[1].is_a?(Types::Bases::Bottom)
else
value(ret, type.args[1])
end

value_check && return_check
end
else
Test.call(val, IS_AP, klass)
Expand Down
2 changes: 2 additions & 0 deletions test/rbs/test/type_check_test.rb
Expand Up @@ -60,6 +60,8 @@ def to_int: () -> Integer

assert typecheck.value([1,2,3].each, parse_type("Enumerator[Integer, Array[Integer]]"))
assert typecheck.value(loop, parse_type("Enumerator[nil, bot]"))
assert typecheck.value([].each, parse_type("Enumerator[String, Array[String]]"))
assert typecheck.value([].each, parse_type("Enumerator[Integer, Array[Integer]]"))

assert typecheck.value(true, parse_type("bool"))
assert typecheck.value(false, parse_type("bool"))
Expand Down
4 changes: 1 addition & 3 deletions test/stdlib/String_test.rb
Expand Up @@ -959,12 +959,10 @@ def test_gsub
end

def test_gsub!
omit 'There is currently a bug that prevents `.gsub!` from being testable'

with_string('l').and /l/ do |pattern|
assert_send_type '(Regexp | string) -> Enumerator[String, String]',
+'hello', :gsub!, pattern
assert_send_type '(Regexp | string) -> Enumerator[String, String]',
assert_send_type '(Regexp | string) -> Enumerator[String, nil]',
+'heya', :gsub!, pattern

assert_send_type '(Regexp | string) { (String) -> _ToS } -> String',
Expand Down