首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这种复数类别名称的方法并不在所有情况下都有效?

为什么这种复数类别名称的方法并不在所有情况下都有效?
EN

Stack Overflow用户
提问于 2010-07-22 19:44:19
回答 1查看 62关注 0票数 1
代码语言:javascript
复制
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

测试应该和期望应该如下所示:

代码语言:javascript
复制
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

最后一次测试失败:(

帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-22 20:43:59

我想你的问题是以你的情况

代码语言:javascript
复制
(PLURALIZATION_EXCEPTIONS.include?(e.downcase)  || e.match(/^[A-Z]+S$/) ||
      e.match(/[memory|hardware]/) )? e : e.pluralize

"perfume"/[memory|hardware]/匹配。

[memory|hardware]是与memor等中的任何一个匹配的character class

也许你指的是e.match(/(memory|hardware)]/i)?这个替代模式可以通过您的测试,但是它不使用您的PLURALIZATION_EXCEPTIONS常量,因此如果您添加了任何其他异常,则需要进行更新。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3308438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档