Is there a pluralizing function in Ruby NOT Rails?

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

found 1 match found 2 matches 

I have Rails installed, maybe I can 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 that I can require to handle this if the script is not Rails but I have Rails installed?

Edit: All of these answers were close, but I checked the one that worked for me. Try this method as an aid to writing Ruby, not Rails, code:

 def pluralize(number, text) return text.pluralize if number != 1 text end 
+43
ruby require pluralize
Mar 15 2018-10-15T00:
source share
7 answers

In fact, all you have to do is

 require 'active_support/inflector' 

and this will expand the type of String.

You can 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 required.

+59
Mar 16 '10 at 10:29
source share

Inflector is redundant 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 where 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 
+51
Jun 13 '10 at 3:36
source share

I personally like a linguistic stone that is definitely not related to rails.

 # from it frontpage require 'linguistics' Linguistics.use :en "box".en.plural #=> "boxes" "mouse".en.plural #=> "mice" # etc 
+10
May 2 '11 at 16:14
source share

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" 
+2
Jan 06 '15 at 20:06
source share
 require 'active_support' require 'active_support/inflector' inf = ActiveSupport::Inflector::Inflections.new 

to get an inflector, not sure how you use it

+1
Mar 15 '10 at 10:35
source share

I have defined a helper function for this, I use it for each user editable kind of model index:

  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 can add this to your YAML language files:

  activerecord: models: post: "Conversation" 
0
Feb 15 '11 at 17:45
source share

my decision:

 # 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 return a β€œreview” if you call cpluralize (1, β€œreviews”)

Hope this helps.

0
Dec 14 2018-11-12T00:
source share



All Articles