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

Implement StringLiteral#count #7239

Merged

Conversation

Blacksmoke16
Copy link
Member

Implements StringLiteral#count.

@bcardiff
Copy link
Member

I’m courious, was this feature needed in some use case?

@Blacksmoke16
Copy link
Member Author

Blacksmoke16 commented Dec 31, 2018

I needed it to count the number of placeholder values in a URL path, i.e. "/users/:user_id/posts/:post_id".count(':').

Other way to do it would be like "path".chars.select { |chr| chr == ':' }.size. This way is quite a bit cleaner.

@j8r
Copy link
Contributor

j8r commented Dec 31, 2018

@Blacksmoke16 you can do "/users/:user_id/posts/:post_id".split(':').size}}

@straight-shoota
Copy link
Member

At some point we need to establish some consistent policy how to decide which methods should be available in the macro language. We shouldn't port all of String to StringLiteral.

But I don't see a reason why not to merge this and it has a use case. There are workarounds, but they obscure meaning.

@asterite
Copy link
Member

asterite commented Jan 2, 2019

Yes, right now there's no clear policy about which macro methods should exit. However, we should probably have all methods from string and all non-mutating methods from array/hash to allow simpler macro code to be written.

@straight-shoota straight-shoota merged commit 5502b00 into crystal-lang:master Jan 2, 2019
@straight-shoota straight-shoota added this to the 0.27.1 milestone Jan 2, 2019
@Blacksmoke16 Blacksmoke16 deleted the StringLiteral-count branch January 2, 2019 13:03
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.

6 participants