PLURALIZATION_EXCEPTIONS = "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测试应该和期望应该如下所示:
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最后一次测试失败:(
帮助!
发布于 2010-07-22 20:43:59
我想你的问题是以你的情况
(PLURALIZATION_EXCEPTIONS.include?(e.downcase) || e.match(/^[A-Z]+S$/) ||
e.match(/[memory|hardware]/) )? e : e.pluralize"perfume"与/[memory|hardware]/匹配。
[memory|hardware]是与m、e、m、o、r等中的任何一个匹配的character class。
也许你指的是e.match(/(memory|hardware)]/i)?这个替代模式可以通过您的测试,但是它不使用您的PLURALIZATION_EXCEPTIONS常量,因此如果您添加了任何其他异常,则需要进行更新。
https://stackoverflow.com/questions/3308438
复制相似问题