Issue #10927 has been updated by zimba tm.


Alright but a change doesn't necessarily need to be substantial to improve developer happiness. It seems like my little hack has two aspects to it that I would like to address separately.

`"foo".gsub("o")` currently doesn't do any substitution. In my point of view it's a hack to get an Enumerator instead of an Array (which #scan returns) and should therefor be deprecated to leave room for other usages (like the one I proposed). I am happy to propose a patch that adds a warning on that usage.

There's a lot of times where I just want to remove some string or regexp match from a string and just being able to `some_string.gsub(pattern)` would make me happy. I don't think it's a too big change if backward-compatibility is not broken. That will have to wait until the first point is resolved.

----------------------------------------
Feature #10927: [PATCH] Add default empty string to string replacements
https://bugs.ruby-lang.org/issues/10927#change-51758

* Author: zimba tm
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Hi ruby devs !

A common case for string substitution is to just remove the found items. This patch changes the `String#[g]sub[!]` methods to allow that.

Old usages:

    "foo".sub("o", "") #=> "fo"
    "foo".gsub("o", "") #=> "f"

New usages:

    "foo".sub("o") #=> "fo"
    "foo".gsub("o") #=> "f"

Applies to the bang versions as well.

This commit changes the return values of String#gsub! call with a single
argument and no block from an Enumerator to a String. The previous usage
would return an Enumerator containing each match, which is not a string
substitution. A suitable replacement should nonetheless be found before
merging this commit.

Before:

    "foo".gsub("o") #=> #<Enumerator: "foo":gsub("o")>

Replacement:

    ???



---Files--------------------------------
0001-Add-a-default-empty-string-to-string-replacements.patch (2.2 KB)


-- 
https://bugs.ruby-lang.org/