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
?
Symbols are objects that can be passed around like any other Ruby object. They can also be used to pass values to methods, such as in getter and setter methods in class definitions: These are just a few examples of when to use symbols in Ruby.
In Ruby, the at-sign ( @ ) before a variable name (e.g. @variable_name ) is used to create a class instance variable.
Symbol is the most basic Ruby object we can create. It's just a name and an internal ID. Since a given symbol name refers to the same object throughout a Ruby program, Symbols are useful and more efficient than strings.
You can pluralize a String
, which represents actual text. Symbol
s 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
Nope, that's the way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With