Make String#chomp! raise ArgumentError for 2+ arguments if string is empty

String#chomp! returned nil without checking the number of passed
arguments in this case.
This commit is contained in:
Jeremy Evans 2023-12-12 16:58:20 -08:00
parent c42e4a38e9
commit 0d53dba7ce
2 changed files with 3 additions and 1 deletions

View File

@ -9689,7 +9689,7 @@ rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
{
VALUE rs;
str_modifiable(str);
if (RSTRING_LEN(str) == 0) return Qnil;
if (RSTRING_LEN(str) == 0 && argc < 2) return Qnil;
rs = chomp_rs(argc, argv);
if (NIL_P(rs)) return Qnil;
return rb_str_chomp_string(str, rs);

View File

@ -587,6 +587,8 @@ CODE
assert_equal("foo", s.chomp!("\n"))
s = "foo\r"
assert_equal("foo", s.chomp!("\n"))
assert_raise(ArgumentError) {String.new.chomp!("", "")}
ensure
$/ = save
$VERBOSE = verbose