9

This works, but looks a little bit ugly:

s = :shop
s.to_s.pluralize.to_sym   # => :shops

Is there a nicer way to pluralize a Symbol ?

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

3 Answers3

12

You can pluralize a String, which represents actual text. Symbols are a bit more abstract.

So, by definition, no. However, perhaps you could open up the Symbol class definition and add:

class Symbol
  def pluralize
    to_s.pluralize.to_sym
  end
end

Then, you can just call:

:shop.pluralize # => :shops
Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
6

Nope, that's the way.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
3

If you're comfortable altering Ruby's classes, then this works:

class Symbol
  def pluralize
    self.to_s.pluralize.to_sym
  end
end

I have yet to find a more elegant solution, although I suspect if there was, it would probably just be Rails implementing something similar to what I have above.

maxluzuriaga
  • 1,327
  • 9
  • 16