[ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix`

Issue #21346 has been reported by matheusrich (Matheus Richard). ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). If approved, I can add `String#ensure_prefix` and the bang versions of those methods. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113304 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by duerst (Martin Dürst). 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!"? ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113316 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard).
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 it is currently implemented, it returns `"Helloo!"` ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113323 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by nobu (Nobuyoshi Nakada). 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? ```ruby "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. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113325 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). @nobu added! Thanks for the review. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113332 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by Eregon (Benoit Daloze). I think this would be good to add (both methods). ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113510 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). Related: I've proposed both methods to Crystal, and the recently were [approved and merged](https://github.com/crystal-lang/crystal/pull/15782#event-17927216823). ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113525 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by maxfelsher (Max Felsher). I like this idea, but I'm concerned about the same method returning `self` in one case and a new string in another. It seems like it'd be easier to reason about if it either always returned a new string or always affected the original object. Or if there's a bang version, it would be consistent with `chomp`, `sub`, etc., for the non-bang version to always return a new string and the bang version to always affect the original object. Apologies if I'm missing something! ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113576 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matz (Yukihiro Matsumoto). `ensure_suffix` accepted. IMO, it should always copy the original string, even when it ends with the suffix. I don't see any reason to add bang-version, right now. Keep it on-demand (because we are lazy). Matz. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113606 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by Eregon (Benoit Daloze). What about `ensure_prefix`? I think it's good to have parity here, I would be surprised if that doesn't exist but `ensure_suffix` does. Also we have `start_with?`/`end_with?`, etc. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113625 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). I've updated the PR as per Matz's comment. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113646 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by ufuk (Ufuk Kayserilioglu). I personally find the `"Hello".ensure_suffix("o!")` case returning `"Helloo!"` very unexpected. I would have expected the change in the string to be the minimal operation needed to ensure that the string ends with the given suffix (i.e. for the result of the example to be `"Hello!"`), but this isn't what's happening. @matz Is this the behaviour that you want? ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113650 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). @ufuk see [dev meeting discussion](https://github.com/ruby/dev-meeting-log/blob/50ce075f492578c2b97a32887adbfc7...)
matz: "Hello".ensure_suffix("o!") should return "Helloo!"
---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113651 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by ufuk (Ufuk Kayserilioglu). matheusrich (Matheus Richard) wrote in #note-13:
@ufuk see [dev meeting discussion](https://github.com/ruby/dev-meeting-log/blob/50ce075f492578c2b97a32887adbfc7...)
matz: "Hello".ensure_suffix("o!") should return "Helloo!"
Ah, ok, I hadn't read the dev meeting discussion. Thanks for the pointer. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113655 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by mame (Yusuke Endoh). Let me record the background of the discussion. During the dev meeting, we considered the following use case: ```ruby path.ends_with?(".rb") ? path : "#{path}.rb" ``` When rewriting this as `path.ensure_suffix(".rb")`, the behavior of appending just `"b"` when path is `"foo.r"` to make it `"foo.rb"` is somewhat unexpected. This is the rationale behind the idea that `"Hello".ensure_suffix("o!")` should return `"Helloo!"`. --- That said, on a personal note, I think the method name `ensure_suffix` is indeed confusing. As @ufuk pointed out, and as @akr also mentioned in the meeting, it sounds like a method that makes the minimal necessary addition. I agree with you. The code snippet `path.ends_with?(".rb") ? path : "#{path}.rb"` isn't particularly long, and its meaning is quite clear, so maybe we don't need this method at all. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113657 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). I think `path.ends_with?(".rb") ? path : "#{path}.rb"` is much more common than the version that just appends `b` to `"foo.r"`. IMO we should optimize for the common operation, which is more predictable and simple as well. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113674 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). Some alternative names: - `with_suffix` - `end_with`: pairs well with `end_with?`, but might be too similar and error prone? - `add_suffix`: I think it implies that the suffix is always added, which is not true. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113675 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by Eregon (Benoit Daloze). I think `with_suffix` would be a bit clearer for the implemented semantics. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113751 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matz (Yukihiro Matsumoto). I don't think other candidates are better. Probably typical usage `str.ensure_suffix(".rb")` helps you understand the behavior. Matz. ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-113984 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/

Issue #21346 has been updated by matheusrich (Matheus Richard). I've updated the documentation examples to reflect the typical use: ```rb "script".ensure_suffix(".rb") # => "script.rb" "script.rb".ensure_suffix(".rb") # => "script.rb" "script.r".ensure_suffix(".rb") # => "script.r.rb" ``` ---------------------------------------- Feature #21346: Introduce `String#ensure_suffix` https://bugs.ruby-lang.org/issues/21346#change-114024 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- ## Problem Ensuring a string has a specific suffix or prefix is a common operation in many applications. Bundler itself uses it: https://github.com/rubygems/rubygems/blob/d409ec8b5fc647fabe30e37e17cd1ea857... Here are [GitHub search](https://github.com/search) queries that might find this pattern in other places: 1. for Ruby: `/end(?:s)?_with\?\(['"].*['"]\) \?/ lang:ruby -is:fork` 1. 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](https://github.com/boltops-tools/ufo/blob/796104fdb89163d09a58fad42add697923...), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006de...))). I've gone ahead and implemented `String#ensure_suffix` in a [pull request](https://github.com/ruby/ruby/pull/13366) but the suggested behavior is this: ```rb "Hell".ensure_suffix("o!") # => "Hello!" "Hello!".ensure_suffix("o!") # => "Hello!" s = "Hello!" s.ensure_suffix("!").equal?(s) # => true # returns same object if already suffixed ``` -- https://bugs.ruby-lang.org/
participants (8)
-
duerst
-
Eregon (Benoit Daloze)
-
mame (Yusuke Endoh)
-
matheusrich (Matheus Richard)
-
matz (Yukihiro Matsumoto)
-
maxfelsher (Max Felsher)
-
nobu (Nobuyoshi Nakada)
-
ufuk (Ufuk Kayserilioglu)