-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
plural? and singular? methods for String #6802
Conversation
# 'post'.singular? # => true | ||
# 'posts'.singular? # => false | ||
# 'sheep'.singular? # => true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should delete the empty lines.
Actually, it's not that trivial. We could expect that any plural value is not singular. |
I think that's okay though, isn't it? Some words are both the plural and
|
Sorry, reading my original description, I see where this may have been a bit confusing. I didn't mean I wanted to check if a word was exclusively either singular or was plural, having it return true in both cases for words with the same form would be okay. |
I looked at this I thought, "Hey, I tried to do this before and I thought this is not supposed to work...". In case someone else is wondering about the same thing, here's why: In Rails <= 3.2: >> "address".singularize
=> "addres" For the longest time, (given the "frozen" status of the inflector) I thought this is how it's supposed to be (i.e. singularize is not idempotent), hence what @butzopower proposed would not work. It turns out this is actually a bug and it was fixed in master in #4719. Based on the test cases in activesupport/test/inflector_test.rb, it does appear the infectors are supposed to be idempotent - at least that's the intention. Therefore what @butzopower proposed is supposed to work on master. |
However, we need to make sure people who uses this understands that this is only as reliable as the infector itself. Edge cases that would normally break the infector would break this also. (Couldn't think of any off the top of my head, most infector "bugs" are around x.pluralize.singularize != x, or the other way around.) The implications are:
Edit: Finally I have an example "edge case" for you: >> 'bonus'.singularize == 'bonus'
=> false This proves my point that people should not expect this to work for arbitrary input. If you try, you definitely will run into problems very quickly. |
end | ||
|
||
assert(!"search".plural?) | ||
assert("fish".plural?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why these extra assertions? They should be covered in SingularToPlural already.
- Consider mirroring the test cases in https://github.com/rails/rails/blob/master/activesupport/test/inflector_test.rb#L45-71, so that when each of the words are tested separately in their own test case. i.e. When one or more of them failed, the rest will still be tested instead of skipped.
Edit: Nvm my point #2. I was looking at the wrong file (inflector_test.rb). What you are doing here seems to be the norm for this file, so that's probably fine. The extra "search" and "fish" should still be removed though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized later that the fish test was redundant.
For "search" I was trying to assert that the method returns false when the string is not the plural form, just to have a negative assertion so that I couldn't make the test pass by just always returning true. I could mock the inflector call to return a different string, if that's preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense.
Is there anything else needed on this? I agree that people should not be using it in validations or really any place that has to do with managing persistance. I think some useful cases would be in helpers / presenters. I can up the documentation to make this more clear, but I think there's already a pretty implicit heed to be cautious around the inflector in general. The issue with bonus seems like a separate issue around inflections, and these methods would return valid results if it were fixed. |
end | ||
|
||
assert(!"searches".singular?) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these assertions would look better without ()
, as in the inflector, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
I think it's a fine addition since we have |
I assumed this goes in the in the 4.0 changelog, that was right, right? |
Since you branched off master, yes it should go into 4.0 changelog. You might also want to get ready for a clean merge by rebasing against master and squashing commits. Also, take a look at the new instructions for formatting the commit message at http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#commit-your-changes :) |
Alright, squashed and rebased on top of latest rails/master. Lemme know if there's anything else I need to do. |
/cc @fxn We'd like to hear your feedback. |
👍 This needs a rebase. |
Methods to determine if a String is plural or singular by comparing it to its inflected form. The methods are defined in the Inflector itself and then called from methods in the String core extension.
@frodsan Done. |
I'm not a fan of this feature because I don't think a lot of applications will get advantages of it. Also the implementation is so simple that you can add in your application. The inflector can return some false positive results and this feature will break, leading more pull requests "fixing" the inflectors. That said I'm closing this one. Thank you so much for the contribution. |
I wanted to know if a string was plural or singular, so I added string extensions.