Feature #21346
openIntroduce `String#ensure_suffix`
Description
Problem¶
Ensuring a string has a specific suffix or prefix is a common operation in many applications.
Bundler itself uses it:
Here are GitHub search queries that might find this pattern in other places:
- for Ruby:
/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork
- for Crystal (a language very similar to Ruby):
/ends_with\?\(['"].*['"]\) \?/ lang:crystal -is:fork
Suggested solution¶
I believe Ruby would benefit from having a first-class method for this purpose.
I suggest the String#ensure_suffix
and String#ensure_prefix
methods.
I think these names are intuitive enough (here are 2 examples of people using ensure
for this purpose (1, 2)).
I've gone ahead and implemented String#ensure_suffix
in a pull request but the suggested behavior is this:
"Hell".ensure_suffix("o!") # => "Hello!"
"Hello!".ensure_suffix("o!") # => "Hello!"
s = "Hello!"
s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed
Updated by matheusrich (Matheus Richard) 7 days ago
If approved, I can add String#ensure_prefix
and the bang versions of those methods.
Updated by duerst (Martin Dürst) 7 days ago
You say "queries that might find this pattern". That seems to say that you haven't found it yet.
What's the result for
"Hello".ensure_suffix("o!")
Is it "Helloo!", or is it "Hello!"?
Updated by matheusrich (Matheus Richard) 6 days ago
· Edited
You say "queries that might find this pattern". That seems to say that you haven't found it yet.
I'm not sure what you mean. I just meant that the regex can find this pattern, but I can't guarantee it will only find that.
What's the result for
"Hello".ensure_suffix("o!")
Is it "Helloo!", or is it "Hello!"?
As currently implemented, it returns "Helloo!"
Updated by nobu (Nobuyoshi Nakada) 6 days ago
matheusrich (Matheus Richard) wrote in #note-3:
You say "queries that might find this pattern". That seems to say that you haven't found it yet.
I'm not sure what you mean. I just meant that the regex can find this pattern, but I can't guarantee it will only find that.
Maybe like this?
"Hell".sub(/(?<!o!)\z/, "o!") #=> "Hello!"
"Hello!".sub(/(?<!o!)\z/, "o!") #=> "Hello!"
"o!Hell".sub(/(?<!o!)\z/, "o!") #=> "o!Hello!"
What's the result for
"Hello".ensure_suffix("o!")
Is it "Helloo!", or is it "Hello!"?
As currently implemented, it returns
"Helloo!"
You should describe the corner case in the doc, and the test preferably.
Updated by matheusrich (Matheus Richard) 5 days ago
@nobu (Nobuyoshi Nakada) added! Thanks for the review.