Issue #10927 has been updated by Martin Drst.


zimba tm wrote:

> `"foo".gsub("o")` currently doesn't do any substitution.

At that point indeed it's just an enumerator. But just try the following:
e = "foo".gsub("o")
=> #<Enumerator: "foo":gsub("o")>
irb(main):010:0> e.each { 'OXO' }
=> "fOXOOXO"
irb(main):011:0> e.each { 'XOX' }
=> "fXOXXOX"

This is definitely doing some substitution. And you can use the same enumerator to create many different new strings. That's what I meant by "lead to new, potentially not yet thought-of usages".


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

* 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/