Skip to content

Add String#ensure_suffix and String#ensure_prefix#15782

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
MatheusRich:string-ensure-suffix
Jun 2, 2025
Merged

Add String#ensure_suffix and String#ensure_prefix#15782
straight-shoota merged 2 commits intocrystal-lang:masterfrom
MatheusRich:string-ensure-suffix

Conversation

@MatheusRich
Copy link
Contributor

@MatheusRich MatheusRich commented May 16, 2025

Closes #15783

This method returns self if 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:

yield relative_filename.ends_with?(".cr") ? relative_filename : "#{relative_filename}.cr"

preopens << {path, path.ends_with?("/") ? path : path + "/", fd}

I'm open to other names, if ensure_suffix isn't clear enough. If this is merged and the core team agrees, I can add ensure_prefix as well.

@straight-shoota
Copy link
Member

I like this, it seems useful.
Please open an issue first to further discuss the feature independent of its implementation (c. f. https://github.com/crystal-lang/crystal/blob/master/CONTRIBUTING.md#using-the-issue-tracker). Thanks 🙇

@MatheusRich
Copy link
Contributor Author

🤦🏽 Sorry. Will do! @straight-shoota

@MatheusRich
Copy link
Contributor Author

Done in #15783

@Blacksmoke16 Blacksmoke16 linked an issue May 16, 2025 that may be closed by this pull request
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.
@MatheusRich MatheusRich changed the title Add String#ensure_suffix Add String#ensure_suffix and String#ensure_prefix May 30, 2025
@MatheusRich MatheusRich force-pushed the string-ensure-suffix branch from 9791f31 to ef7d20c Compare May 30, 2025 14:41
Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could already use these new methods in the locations mentioned in the OP.

@MatheusRich
Copy link
Contributor Author

@straight-shoota do we want to do that as part of this PR?

@straight-shoota
Copy link
Member

Yes, I think this would fit here. Could be a follow-up as well.

@MatheusRich
Copy link
Contributor Author

MatheusRich commented May 30, 2025

@straight-shoota I'd love to do it as follow-up, then (Draft PR here).

@straight-shoota straight-shoota added this to the 1.17.0 milestone May 31, 2025
@straight-shoota straight-shoota merged commit 1f5f366 into crystal-lang:master Jun 2, 2025
36 checks passed
MatheusRich added a commit to MatheusRich/crystal that referenced this pull request Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce String#ensure_suffix and String#ensure_prefix

4 participants