Feature #16666


'string' - 'str' as a shortcut for 'string'.gsub('str', '')?

Added by vladpisanov (Vlad Pisanov) about 2 years ago. Updated about 2 years ago.

Target version:


Sorry if this has been brought up before, but I couldn't find anything.

I love how we can substract arrays ([1,2,3] - [1,2]). Does anyone else think the same syntax would be beneficial for Strings?

Some possible scenarios could be:

'string' - 'str'       # Remove substring
'string' - ['s', 't']  # Remove a set of characters
'string' - /^st/       # Remove by regexp

Updated by sawa (Tsuyoshi Sawada) about 2 years ago

For removing a set of characters, there is already String#delete. And for your other cases, there is a similar proposal

Updated by shevegen (Robert A. Heiler) about 2 years ago

Does anyone else think the same syntax would be beneficial for Strings?

I do not doubt that it can be useful. One advantage of your proposal is that it
is very short and succinct.

Personally, though, I prefer .gsub() or .gsub!(), and .delete or sawa's suggestion.

I don't have anything against "-" per se and as you showed that Array has the
method, the same could perhaps be used for class String. But this may be a design
decision (I don't know).

But perhaps there is a design consideration matz considered (or not, I really do
not know). It may be best to ask matz about - for class String.

I would also recommend to contrast your proposal to what sawa suggested; for
example, you could propose - to be an alias of String#delete, or something
else. That may be helpful or perhaps simpler. (I myself really don't know)


Also available in: Atom PDF