Project

General

Profile

Actions

Feature #16666

open

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

Added by vladpisanov (Vlad Pisanov) over 4 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:97316]

Description

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) over 4 years ago

For removing a set of characters, there is already String#delete. And for your other cases, there is a similar proposal https://bugs.ruby-lang.org/issues/12698.

Updated by shevegen (Robert A. Heiler) over 4 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)

Actions

Also available in: Atom PDF

Like0
Like0Like0