提交 4535191c 编写于 作者: R Ryan Oblak

Modified String#pluralize to take an optional count parameter.

上级 c612183a
......@@ -9,15 +9,26 @@
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
# "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.
#
......
......@@ -64,6 +64,10 @@ def test_pluralize
end
assert_equal("plurals", "plurals".pluralize)
assert_equal("blargles", "blargle".pluralize(0))
assert_equal("blargle", "blargle".pluralize(1))
assert_equal("blargles", "blargle".pluralize(2))
end
def test_singularize
......
......@@ -1426,6 +1426,14 @@ The method +pluralize+ returns the plural of its receiver:
As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Built-in rules can be extended in +config/initializers/inflections.rb+. That file is generated by the +rails+ command and has instructions in comments.
+pluralize+ can also take an optional +count+ parameter. If <tt>count == 1</tt> the singular form will be returned. For any other value of +count+ the plural form will be returned:
<ruby>
"dude".pluralize(0) # => "dudes"
"dude".pluralize(1) # => "dude"
"dude".pluralize(2) # => "dudes"
</ruby>
Active Record uses this method to compute the default table name that corresponds to a model:
<ruby>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册