Add String#ensure_suffix and String#ensure_prefix#15782
Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom Jun 2, 2025
Merged
Add String#ensure_suffix and String#ensure_prefix#15782straight-shoota merged 2 commits intocrystal-lang:masterfrom
String#ensure_suffix and String#ensure_prefix#15782straight-shoota merged 2 commits intocrystal-lang:masterfrom
Conversation
Member
|
I like this, it seems useful. |
Contributor
Author
|
🤦🏽 Sorry. Will do! @straight-shoota |
Contributor
Author
|
Done in #15783 |
This method returns `self` if the string ends with the given suffix. Otherwise, returns a new string with the suffix appended.
This method returns `self` if the string starts with the given prefix. Otherwise, returns a new string with the prefix prepended.
String#ensure_suffixString#ensure_suffix and String#ensure_prefix
9791f31 to
ef7d20c
Compare
straight-shoota
approved these changes
May 30, 2025
Member
straight-shoota
left a comment
There was a problem hiding this comment.
We could already use these new methods in the locations mentioned in the OP.
Contributor
Author
|
@straight-shoota do we want to do that as part of this PR? |
Member
|
Yes, I think this would fit here. Could be a follow-up as well. |
Contributor
Author
|
@straight-shoota I'd love to do it as follow-up, then (Draft PR here). |
MatheusRich
added a commit
to MatheusRich/crystal
that referenced
this pull request
May 30, 2025
ysbaddaden
approved these changes
May 31, 2025
1 task
MatheusRich
added a commit
to MatheusRich/crystal
that referenced
this pull request
Jul 31, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #15783
This method returns
selfif the string ends with the given suffix. Otherwise, returns a new string with the suffix appended.I've seen this pattern repeated several times along the years. Crystal itself does it in at least two places:
crystal/src/compiler/crystal/crystal_path.cr
Line 132 in 9791f31
crystal/src/crystal/system/wasi/wasi.cr
Line 27 in 9791f31
I'm open to other names, if
ensure_suffixisn't clear enough. If this is merged and the core team agrees, I can addensure_prefixas well.