我正在尋找這樣的功能:
collection([1,2,3]) # '1, 2, and 3.'
我認為函式集合或類似的東西在 Rails 助手中的某處定義了,但我找不到它。
任何人都可以對此有所了解嗎?
uj5u.com熱心網友回復:
我想你正在尋找 to_sentence
https://apidock.com/rails/Array/to_sentence
uj5u.com熱心網友回復:
to_sentence 會做你問的大部分事情:
[1,2,3].to_sentence # => "1, 2, and 3"
標點符號需要自己加:
[1,2,3].to_sentence "." # => "1, 2, and 3."
# OR
"#{[1,2,3].to_sentence}." # => "1, 2, and 3."
如果您希望句子大寫(不適用于您的示例),您可以使用.humanize:
["one", "two", "three"].to_sentence # => "one, two, and three"
["one", "two", "three"].to_sentence.humanize # => "One, two, and three"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/384612.html
標籤:红宝石轨道
上一篇:如何用圖示寫繭洗掉鏈接
