diff options
author | Ryan Oblak <rroblak@gmail.com> | 2011-09-27 22:39:31 -0700 |
---|---|---|
committer | Ryan Oblak <rroblak@gmail.com> | 2011-09-28 12:24:06 -0700 |
commit | 4535191c61b496b1a5e9dc57624581753fa71497 (patch) | |
tree | ee10fb199b2a43f4eadb6cf913d499e133bf59e6 /activesupport/lib/active_support/core_ext/string/inflections.rb | |
parent | c612183ad19f2c11eb58e4393a759a1016ce4c05 (diff) | |
download | rails-4535191c61b496b1a5e9dc57624581753fa71497.tar.gz rails-4535191c61b496b1a5e9dc57624581753fa71497.tar.bz2 rails-4535191c61b496b1a5e9dc57624581753fa71497.zip |
Modified String#pluralize to take an optional count parameter.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/string/inflections.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/inflections.rb | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index c7ceeb9de4..fd91b3cacb 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -9,14 +9,25 @@ require 'active_support/inflector/transliterate' class String # Returns the plural form of the word in the string. # + # If the optional parameter +count+ is specified, + # the singular form will be returned if <tt>count == 1</tt>. + # For any other value of +count+ the plural will be returned. + # + # ==== Examples # "post".pluralize # => "posts" # "octopus".pluralize # => "octopi" # "sheep".pluralize # => "sheep" # "words".pluralize # => "words" # "the blue mailman".pluralize # => "the blue mailmen" # "CamelOctopus".pluralize # => "CamelOctopi" - def pluralize - ActiveSupport::Inflector.pluralize(self) + # "apple".pluralize(1) # => "apple" + # "apple".pluralize(2) # => "apples" + def pluralize(count = nil) + if count == 1 + self + else + ActiveSupport::Inflector.pluralize(self) + end end # The reverse of +pluralize+, returns the singular form of a word in a string. @@ -42,7 +53,7 @@ class String def constantize ActiveSupport::Inflector.constantize(self) end - + # +safe_constantize+ tries to find a declared constant with the name specified # in the string. It returns nil when the name is not in CamelCase # or is not initialized. See ActiveSupport::Inflector.safe_constantize |