ruby
54 lines · 8 steps
Building an English pluralizer in Ruby
A layered lookup of irregulars, uncountables, and regex rules turns any singular noun into its plural form.
Explained by
highlit
1class Pluralizer
2 IRREGULARS = {
3 "person" => "people",
4 "child" => "children",
5 "foot" => "feet",
6 "tooth" => "teeth",
7 "mouse" => "mice",
8 "goose" => "geese",
9 "man" => "men",
10 "woman" => "women"
11 }.freeze
12
13 UNCOUNTABLE = %w[fish sheep series species deer equipment information].freeze
14
15 RULES = [
16 [/(matr|vert|ind)(?:ix|ex)$/i, '\1ices'],
17 [/(x|ch|ss|sh)$/i, '\1es'],
18 [/([^aeiouy]|qu)y$/i, '\1ies'],
19 [/(?:([^f])fe|([lr])f)$/i, '\1\2ves'],
20 [/sis$/i, 'ses'],
21 [/(bu)s$/i, '\1ses'],
22 [/([ti])um$/i, '\1a'],
23 [/s$/i, 's'],
24 [/$/, 's']
25 ].freeze
26
27 def self.pluralize(count, word)
28 "#{count} #{count.abs == 1 ? word : plural(word)}"
29 end
30
31 def self.plural(word)
32 return word if UNCOUNTABLE.include?(word.downcase)
33
34 if (irregular = IRREGULARS[word.downcase])
35 return match_case(word, irregular)
36 end
37
38 RULES.each do |pattern, replacement|
39 return word.sub(pattern, replacement) if word =~ pattern
40 end
41
42 word
43 end
44
45 def self.match_case(original, replacement)
46 if original == original.upcase
47 replacement.upcase
48 elsif original == original.capitalize
49 replacement.capitalize
50 else
51 replacement
52 end
53 end
54end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Ordering rules from most specific to a catch-all lets a single pass handle both exceptions and the common case.
- 2Separating irregular words and uncountables into constant tables keeps the regex logic clean and readable.
- 3Preserving the original word's casing after transformation makes a helper reusable across any capitalization.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-an-english-pluralizer-in-ruby-explained-ruby-0c0e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.