Is there a pluralize function in Ruby NOT Rails?

RubyRequirePluralize

Ruby Problem Overview


I am writing some Ruby code, not Rails, and I need to handle something like this:

found 1 match
found 2 matches

I have Rails installed so maybe I might be able to add a require clause at the top of the script, but does anyone know of a RUBY method that pluralizes strings? Is there a class I can require that can deal with this if the script isn't Rails but I have Rails installed?

Edit: All of these answers were close but I checked off the one that got it working for me. Try this method as a helper when writing Ruby, not Rails, code:

def pluralize(number, text)
  return text.pluralize if number != 1
  text
end

Ruby Solutions


Solution 1 - Ruby

Actually all you need to do is

require 'active_support/inflector'

and that will extend the String type.

you can then do

"MyString".pluralize

which will return

"MyStrings"

for 2.3.5 try:

require 'rubygems'
require 'active_support/inflector'

should get it, if not try

sudo gem install activesupport

and then the requires.

Solution 2 - Ruby

Inflector is overkill for most situations.

def x(n, singular, plural=nil)
    if n == 1
        "1 #{singular}"
    elsif plural
        "#{n} #{plural}"
    else
        "#{n} #{singular}s"
    end
end

Put this in common.rb, or wherever you like your general utility functions and...

require "common" 

puts x(0, 'result') # 0 results
puts x(1, 'result') # 1 result
puts x(2, 'result') # 2 results

puts x(0, 'match', 'matches') # 0 matches
puts x(1, 'match', 'matches') # 1 match 
puts x(2, 'match', 'matches') # 2 matches 

Solution 3 - Ruby

I personally like the linguistics gem that is definitely not rails related.

# from it's frontpage
require 'linguistics'

Linguistics.use :en

"box".en.plural #=> "boxes"
"mouse".en.plural #=> "mice"
# etc

Solution 4 - Ruby

This works for me (using ruby 2.1.1 and actionpack 3.2.17):

~$ irb
>> require 'action_view'
=> true
>> include ActionView::Helpers::TextHelper
=> Object
>> pluralize(1, 'cat')
=> "1 cat"
>> pluralize(2, 'cat')
=> "2 cats"

Solution 5 - Ruby

require 'active_support'
require 'active_support/inflector'

inf = ActiveSupport::Inflector::Inflections.new

to get the inflector, not sure how you use it

Solution 6 - Ruby

my solution:

# Custom pluralize - will return text without the number as the default pluralize.
def cpluralize(number, text)
  return text.pluralize if number != 1 
  return text.singularize if number == 1
end

So you can have 'review' returned if you call cpluralize(1, 'reviews')

Hope that helps.

Solution 7 - Ruby

I've defined a helper function for that, I use it for every user editable model's index view :

  def ovyka_counter(array, name=nil, plural=nil)
    name ||= array.first.class.human_name.downcase
    pluralize(array.count, name, plural)
  end

then you can call it from the view :

<% ovyka_counter @posts %>

for internationalization (i18n), you may then add this to your locale YAML files :

  activerecord:
    models:
      post: "Conversation"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaaronaView Question on Stackoverflow
Solution 1 - RubyscaneyView Answer on Stackoverflow
Solution 2 - RubyeukrasView Answer on Stackoverflow
Solution 3 - RubyhawxView Answer on Stackoverflow
Solution 4 - RubyEffectiveQAView Answer on Stackoverflow
Solution 5 - RubyscaneyView Answer on Stackoverflow
Solution 6 - RubyJesse NguyenView Answer on Stackoverflow
Solution 7 - RubyVikoAlucardView Answer on Stackoverflow