如何在 Ruby (on Rails) 中对符号进行复数形式?
这可行,但看起来有点难看:
s = :shop
s.to_s.pluralize.to_sym # => :shops
是否有更好的方法来复数 Symbol
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以复数一个
String
,它代表实际的文本。Symbol
更加抽象一些。所以,根据定义,不。但是,也许您可以打开 Symbol 类定义并添加:
然后,您可以调用:
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:
Then, you can just call:
不,就是这样。
Nope, that's the way.
如果您愿意改变 Ruby 的类,那么这可行:
我还没有找到更优雅的解决方案,尽管我怀疑如果有的话,它可能只是 Rails 实现与我上面类似的东西。
If you're comfortable altering Ruby's classes, then this works:
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.