ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix`
@ 2025-05-16 20:00 matheusrich (Matheus Richard) via ruby-core
  2025-05-16 20:02 ` [ruby-core:122155] " matheusrich (Matheus Richard) via ruby-core
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-05-16 20:00 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122155] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
@ 2025-05-16 20:02 ` matheusrich (Matheus Richard) via ruby-core
  2025-05-17  8:24 ` [ruby-core:122167] " duerst via ruby-core
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-05-16 20:02 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122167] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
  2025-05-16 20:02 ` [ruby-core:122155] " matheusrich (Matheus Richard) via ruby-core
@ 2025-05-17  8:24 ` duerst via ruby-core
  2025-05-17 16:30 ` [ruby-core:122174] " matheusrich (Matheus Richard) via ruby-core
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: duerst via ruby-core @ 2025-05-17  8:24 UTC (permalink / raw)
  To: ruby-core; +Cc: duerst

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122174] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
  2025-05-16 20:02 ` [ruby-core:122155] " matheusrich (Matheus Richard) via ruby-core
  2025-05-17  8:24 ` [ruby-core:122167] " duerst via ruby-core
@ 2025-05-17 16:30 ` matheusrich (Matheus Richard) via ruby-core
  2025-05-18  3:33 ` [ruby-core:122178] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-05-17 16:30 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122178] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (2 preceding siblings ...)
  2025-05-17 16:30 ` [ruby-core:122174] " matheusrich (Matheus Richard) via ruby-core
@ 2025-05-18  3:33 ` nobu (Nobuyoshi Nakada) via ruby-core
  2025-05-18 17:55 ` [ruby-core:122186] " matheusrich (Matheus Richard) via ruby-core
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2025-05-18  3:33 UTC (permalink / raw)
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122186] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (3 preceding siblings ...)
  2025-05-18  3:33 ` [ruby-core:122178] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2025-05-18 17:55 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-01 20:06 ` [ruby-core:122365] " Eregon (Benoit Daloze) via ruby-core
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-05-18 17:55 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122365] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (4 preceding siblings ...)
  2025-05-18 17:55 ` [ruby-core:122186] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-01 20:06 ` Eregon (Benoit Daloze) via ruby-core
  2025-06-02 12:19 ` [ruby-core:122379] " matheusrich (Matheus Richard) via ruby-core
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-06-01 20:06 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122379] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (5 preceding siblings ...)
  2025-06-01 20:06 ` [ruby-core:122365] " Eregon (Benoit Daloze) via ruby-core
@ 2025-06-02 12:19 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-04  4:04 ` [ruby-core:122403] " maxfelsher (Max Felsher) via ruby-core
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-06-02 12:19 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122403] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (6 preceding siblings ...)
  2025-06-02 12:19 ` [ruby-core:122379] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-04  4:04 ` maxfelsher (Max Felsher) via ruby-core
  2025-06-05  5:40 ` [ruby-core:122426] " matz (Yukihiro Matsumoto) via ruby-core
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: maxfelsher (Max Felsher) via ruby-core @ 2025-06-04  4:04 UTC (permalink / raw)
  To: ruby-core; +Cc: maxfelsher (Max Felsher)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122426] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (7 preceding siblings ...)
  2025-06-04  4:04 ` [ruby-core:122403] " maxfelsher (Max Felsher) via ruby-core
@ 2025-06-05  5:40 ` matz (Yukihiro Matsumoto) via ruby-core
  2025-06-05  9:17 ` [ruby-core:122444] " Eregon (Benoit Daloze) via ruby-core
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-06-05  5:40 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122444] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (8 preceding siblings ...)
  2025-06-05  5:40 ` [ruby-core:122426] " matz (Yukihiro Matsumoto) via ruby-core
@ 2025-06-05  9:17 ` Eregon (Benoit Daloze) via ruby-core
  2025-06-05 14:51 ` [ruby-core:122457] " matheusrich (Matheus Richard) via ruby-core
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-06-05  9:17 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122457] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (9 preceding siblings ...)
  2025-06-05  9:17 ` [ruby-core:122444] " Eregon (Benoit Daloze) via ruby-core
@ 2025-06-05 14:51 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-05 16:57 ` [ruby-core:122462] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-06-05 14:51 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122462] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (10 preceding siblings ...)
  2025-06-05 14:51 ` [ruby-core:122457] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-05 16:57 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2025-06-05 16:59 ` [ruby-core:122463] " matheusrich (Matheus Richard) via ruby-core
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2025-06-05 16:57 UTC (permalink / raw)
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122463] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (11 preceding siblings ...)
  2025-06-05 16:57 ` [ruby-core:122462] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2025-06-05 16:59 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-05 18:18 ` [ruby-core:122467] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-06-05 16:59 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

Issue #21346 has been updated by matheusrich (Matheus Richard).


@ufuk see [dev meeting discussion](https://github.com/ruby/dev-meeting-log/blob/50ce075f492578c2b97a32887adbfc7515e1e1ab/2025/DevMeeting-2025-06-05.md#L4:~:text=matz%3A%20%22Hello%22.ensure_suffix(%22o!%22)%20should%20return%20%22Helloo!%22)

> 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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122467] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (12 preceding siblings ...)
  2025-06-05 16:59 ` [ruby-core:122463] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-05 18:18 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2025-06-05 21:37 ` [ruby-core:122469] " mame (Yusuke Endoh) via ruby-core
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2025-06-05 18:18 UTC (permalink / raw)
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

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/50ce075f492578c2b97a32887adbfc7515e1e1ab/2025/DevMeeting-2025-06-05.md#L4:~:text=matz%3A%20%22Hello%22.ensure_suffix(%22o!%22)%20should%20return%20%22Helloo!%22)
> 
> > 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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122469] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (13 preceding siblings ...)
  2025-06-05 18:18 ` [ruby-core:122467] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2025-06-05 21:37 ` mame (Yusuke Endoh) via ruby-core
  2025-06-06 14:26 ` [ruby-core:122485] " matheusrich (Matheus Richard) via ruby-core
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2025-06-05 21:37 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122485] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (14 preceding siblings ...)
  2025-06-05 21:37 ` [ruby-core:122469] " mame (Yusuke Endoh) via ruby-core
@ 2025-06-06 14:26 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-06 14:33 ` [ruby-core:122486] " matheusrich (Matheus Richard) via ruby-core
  2025-06-13 17:29 ` [ruby-core:122526] " Eregon (Benoit Daloze) via ruby-core
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-06-06 14:26 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122486] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (15 preceding siblings ...)
  2025-06-06 14:26 ` [ruby-core:122485] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-06 14:33 ` matheusrich (Matheus Richard) via ruby-core
  2025-06-13 17:29 ` [ruby-core:122526] " Eregon (Benoit Daloze) via ruby-core
  17 siblings, 0 replies; 19+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2025-06-06 14:33 UTC (permalink / raw)
  To: ruby-core; +Cc: matheusrich (Matheus Richard)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [ruby-core:122526] [Ruby Feature#21346] Introduce `String#ensure_suffix`
  2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
                   ` (16 preceding siblings ...)
  2025-06-06 14:33 ` [ruby-core:122486] " matheusrich (Matheus Richard) via ruby-core
@ 2025-06-13 17:29 ` Eregon (Benoit Daloze) via ruby-core
  17 siblings, 0 replies; 19+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-06-13 17:29 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

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/d409ec8b5fc647fabe30e37e17cd1ea857634f6b/bundler/lib/bundler/uri_normalizer.rb#L17

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/796104fdb89163d09a58fad42add697923c18294/lib/ufo/cfn/stack/builder/resources/dns.rb#L76), [2](https://github.com/mumuki/mumuki-domain/blob/6194089d82b1a0c8805ecba98e006deb1694dc8e/lib/mumuki/domain/extensions/string.rb#L6))).

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/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2025-06-13 17:30 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-16 20:00 [ruby-core:122154] [Ruby Feature#21346] Introduce `String#ensure_suffix` matheusrich (Matheus Richard) via ruby-core
2025-05-16 20:02 ` [ruby-core:122155] " matheusrich (Matheus Richard) via ruby-core
2025-05-17  8:24 ` [ruby-core:122167] " duerst via ruby-core
2025-05-17 16:30 ` [ruby-core:122174] " matheusrich (Matheus Richard) via ruby-core
2025-05-18  3:33 ` [ruby-core:122178] " nobu (Nobuyoshi Nakada) via ruby-core
2025-05-18 17:55 ` [ruby-core:122186] " matheusrich (Matheus Richard) via ruby-core
2025-06-01 20:06 ` [ruby-core:122365] " Eregon (Benoit Daloze) via ruby-core
2025-06-02 12:19 ` [ruby-core:122379] " matheusrich (Matheus Richard) via ruby-core
2025-06-04  4:04 ` [ruby-core:122403] " maxfelsher (Max Felsher) via ruby-core
2025-06-05  5:40 ` [ruby-core:122426] " matz (Yukihiro Matsumoto) via ruby-core
2025-06-05  9:17 ` [ruby-core:122444] " Eregon (Benoit Daloze) via ruby-core
2025-06-05 14:51 ` [ruby-core:122457] " matheusrich (Matheus Richard) via ruby-core
2025-06-05 16:57 ` [ruby-core:122462] " ufuk (Ufuk Kayserilioglu) via ruby-core
2025-06-05 16:59 ` [ruby-core:122463] " matheusrich (Matheus Richard) via ruby-core
2025-06-05 18:18 ` [ruby-core:122467] " ufuk (Ufuk Kayserilioglu) via ruby-core
2025-06-05 21:37 ` [ruby-core:122469] " mame (Yusuke Endoh) via ruby-core
2025-06-06 14:26 ` [ruby-core:122485] " matheusrich (Matheus Richard) via ruby-core
2025-06-06 14:33 ` [ruby-core:122486] " matheusrich (Matheus Richard) via ruby-core
2025-06-13 17:29 ` [ruby-core:122526] " Eregon (Benoit Daloze) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).