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

String pluralization #72

Merged
merged 6 commits into from Apr 24, 2015
Merged

String pluralization #72

merged 6 commits into from Apr 24, 2015

Conversation

jodosha
Copy link
Member

@jodosha jodosha commented Apr 23, 2015

Feature

Introduce pluralization for strings.

How

It adds a new public API method: Utils::String#pluralize, which returns a new pluralized instance of the same type.

string = Lotus::Utils::String.new("flower")
string.pluralize       # => "flowers"
string.pluralize.class # => Lotus::Utils::String

The implementation is delegated to a new module: Lotus::Utils::Inflector. It exposes .pluralize which works with Ruby's String.

Lotus::Utils::Inflector.pluralize("flower")       # => "flowers"
Lotus::Utils::Inflector.pluralize("flower").class # => String

Pluralization is idempotent.

Lotus::Utils::Inflector.pluralize(
  Lotus::Utils::Inflector.pluralize("cars")
) # => "cars"

It respects the case:

Lotus::Utils::Inflector.pluralize("Application") # => "Applications"

Exceptions

English is a really hard language to pluralize. This implementation takes care of a lot of exceptions.

Singular Plural
radix radices
alumnus alumni
criterion criteria
vertebra vertebrae
hero heroes
lumen lumina
stigma stigmata
iris irides
impetus impetuses
lens lenses
bureau bureaux

Benchmark

TL;DR: ActiveSupport pluralization is 2x faster.

  Calculating -------------------------------------
                 utils     8.000  i/100ms
                    AS    20.000  i/100ms
  -------------------------------------------------
                 utils     82.743  (± 2.4%) i/s -    416.000
                    AS    199.489  (± 4.5%) i/s -      1.000k

https://gist.github.com/jodosha/55bafdd83a89741258fd


Ref hanami/router#51

@jodosha jodosha self-assigned this Apr 23, 2015
jodosha added a commit that referenced this pull request Apr 24, 2015
@jodosha jodosha merged commit 48a0f78 into master Apr 24, 2015
@jodosha jodosha deleted the string-pluralization branch April 24, 2015 14:20
AlfonsoUceda pushed a commit to AlfonsoUceda/utils that referenced this pull request Apr 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant