Why does this method for pluralizing category names not work in all cases?
PLURALIZATION_EXCEP开发者_C百科TIONS = "hardware",'memory'
def pluralize_category_name(name)
category = name.split(' and ')
exceptions_to_exp = ""
category.map! { |e|
if e.match(/^[A-Z]+$/) and !e.match(/^[A-Z]+S$/)
e = e.pluralize
end
(PLURALIZATION_EXCEPTIONS.include?(e.downcase) || e.match(/^[A-Z]+S$/) ||
e.match(/[memory|hardware]/) )? e : e.pluralize
}.join(' and ')
end
The test should and expectation should be as follows:
it "properly pluralizes hardware as hardware" do
pluralize_category_name("hardware").should == "hardware"
end
it "properly pluralizes UPS as UPS" do
pluralize_category_name("UPS").should == "UPS"
end
it "properly pluralizes PDA and Portable Hardware as PDAs and Portable Hardware" do
pluralize_category_name("PDA and Portable Hardware").should == "PDAs and Portable Hardware"
end
it "properly pluralizes perfume and cologne as perfumes and colognes" do
pluralize_category_name("perfume and cologne").should == "perfumes and colognes"
end
The last test fails :(
HELP!
I think your problem is that in your condition
(PLURALIZATION_EXCEPTIONS.include?(e.downcase) || e.match(/^[A-Z]+S$/) ||
e.match(/[memory|hardware]/) )? e : e.pluralize
"perfume"
matches /[memory|hardware]/
.
[memory|hardware]
is a character class that matches any of m
, e
, m
, o
, r
, etc..
Perhaps you meant e.match(/(memory|hardware)]/i)
instead? This alternative pattern would pass your tests but it doesn't make use of your PLURALIZATION_EXCEPTIONS
constant so would need updating if you added any other exceptions.
精彩评论