1
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

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!

1 Answer 1

2

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.

1
  • 1
    also, you could do /(#{PLURALIZATION_EXCEPTIONS.join '|'})/ to have the regexp catch any of your defined exceptions. Jul 22, 2010 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.