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

Make the apply_inflections method case-sensitive #14247

Merged
merged 1 commit into from Jun 26, 2014
Merged

Make the apply_inflections method case-sensitive #14247

merged 1 commit into from Jun 26, 2014

Conversation

robin850
Copy link
Member

@robin850 robin850 commented Mar 2, 2014

Hello,

Since d3071db, the apply_inflections method check if the downcased version of a string is contained inside the "whitelist" of uncountable words. However, if the word is composed of capital letters, it won't be
matched in the list while it should.

We can't simply revert to the previous behavior as there is a performance concern (benchmarked over /usr/share/dict/words):

user system total real
Before d3071db 135.610000 0.290000 135.900000 (137.807081)
Since d3071db 22.170000 0.020000 22.190000 ( 22.530005)
With the patch 25.130000 0.070000 25.200000 ( 25.611538)

Benchmarked with http://git.io/aFnWig.

Have a nice day.

@arthurnn
Copy link
Member

arthurnn commented Mar 3, 2014

👍

@robin850
Copy link
Member Author

robin850 commented May 3, 2014

@fxn : Would you mind having a look please ? :-)

@@ -1,3 +1,8 @@
* Make the `apply_inflections` method case-sensitive when checking
Copy link
Member

Choose a reason for hiding this comment

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

Should not it be case-insensitive now?

Copy link
Member Author

Choose a reason for hiding this comment

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

I may be wrong but I think "case-sensitive" is right. At the moment, if we want to make the "HTTP" string uncountable, we'd have to write inflections.uncountable "http" (that would apply to "Http", "HtTp", etc. too) so I'd say that this is case-insensitive currently.

With this patch, half the problem is resolved. Actually if we write inflections.uncountable "HTTP", "HTTP" is defined as an uncountable but "http", "Http", etc. too. There is no easy way to fix this as people may except "Information".pluralize to return "Information" if we have the inflections.uncountable "information rule.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's now case insensitive in both directions; previously it was insensitive to the case of the string being inflected, but very sensitive (as in, only one worked) to the case when the inflection is defined.

@matthewd
Copy link
Member

matthewd commented May 7, 2014

We don't want to just downcase the incoming string at the time the inflection is defined?

@rafaelfranca
Copy link
Member

@matthewd if we do that HTTP and HTTp will be the same and I think they should not.

@matthewd
Copy link
Member

Actually if we write inflections.uncountable "HTTP", "HTTP" is defined as an uncountable but "http", "Http", etc. too.

As I understand it, "HTTP" and "HTTp" are getting treated identically either way: there's only one place we use uncountables, and in that place, we're mapping it through downcase before we use the value. So why bother storing the correctly-cased version? 😕

@rafaelfranca
Copy link
Member

Yes, make sense. I think we should store the downcase version

@robin850
Copy link
Member Author

robin850 commented Jun 1, 2014

@rafaelfranca @matthewd : Your comments have been addressed normally, thanks for your feedback and sorry it took so long to get a response from my side! :-)

I've updated the commit message to reflect the actual performances ; this seems even faster with your suggestion (we are meeting the same performances as it is currently on master).

@@ -160,7 +160,7 @@ def irregular(singular, plural)
# uncountable 'money', 'information'
# uncountable %w( money information rice )
def uncountable(*words)
(@uncountables << words).flatten!
(@uncountables << words.flatten.map(&:downcase)).flatten!
Copy link
Member

Choose a reason for hiding this comment

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

Can we use map!?

Copy link
Member

Choose a reason for hiding this comment

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

And since we already call flatten before the map we still need the flatten!?

Copy link
Member Author

Choose a reason for hiding this comment

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

As for #map!, I'm not really sure. People may pass an array built from a file or the database so it'd be altered but I don't know if this is that common. What do you think ?

As for the two flatten calls, I agree that they don't fit here. Actually this was here because we can pass arrays to the uncoutable method (e.g. here) but the words parameter is still an array so we ended up in having an array of arrays. Another implementation could be:

@uncountables += words.flatten.map(&:downcase)

Which is also a bit faster.

Since d3071db, the apply_inflections method check if the downcased
version of a string is contained inside the "whitelist" of uncountable
words. However, if the word is composed of capital letters, it won't be
matched in the list while it should.

We can't simply revert to the previous behavior as there is a
performance concern (benchmarked over /usr/share/dict/words):

Before d3071db  135.610000   0.290000  135.900000 (137.807081)
Since d3071db   22.170000    0.020000  22.190000  ( 22.530005)
With the patch   22.060000    0.020000  22.080000  ( 22.125771)

Benchmarked with http://git.io/aFnWig

This way, the solution is to put the down-case version of words inside
the @uncountables array.
rafaelfranca added a commit that referenced this pull request Jun 26, 2014
Make the apply_inflections method case-sensitive
@rafaelfranca rafaelfranca merged commit d04763f into rails:master Jun 26, 2014
@robin850 robin850 deleted the inflections-with-uncountables branch June 27, 2014 07:59
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.

None yet

4 participants