Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change regex percent literal delimiter to singlequote #626

Closed

Conversation

Epigene
Copy link

@Epigene Epigene commented Apr 20, 2024

Curly braces are a part of Regex syntax (.{3,}, .{,9} etc.), so if the literal contains curlybraces, I have to use some other unspecified fallback delimiters. Bikeshedding ensues. Let's use delimiters that are not a part of Regex syntax and are less likely to appear in them - singlequotes. This goes nicely with string literals using doublequotes.

@searls
Copy link
Contributor

searls commented Apr 20, 2024

Though I don't use it myself, my understanding is that %r{} is quite common and popular, so this would be a pretty major change. Additionally, I just scanned the regexes in the app that I'm building and a full third of them contain ' characters, so this would be a pretty disruptive change AFAICT.

Would welcome other perspectives here

@benjamineskola
Copy link
Contributor

As I understand it, paired brackets as delimiters don't prevent you from using the same character, unescaped, within the literal:

irb(main):001> %r{a{0,1}}
=> /a{0,1}/

Whereas unpaired characters like ' would require that all ' be escaped:

irb(main):002> %r'it\'s'
=> /it's/

So since %r{} doesn't prevent any characters being used, and %r'' would, I don't think this change would make sense; there's nothing to gain by it.

(In the worst-case scenario, a regex containing an unpaired closing } would require it to be escaped if using %r{}. But that would make it no worse than using %r'' when the regex contains a ', and in any case //-literals are a viable alternative.)

@searls
Copy link
Contributor

searls commented Jul 11, 2024

Makes sense to me, failing a clear counter-argument. Thanks!

@searls searls closed this Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants