Is there a standard rails helper that converts an array of strings like ["apple", "banana", "pear"] into "apple, banana, and pear" for inserting into a sentence?
2 Answers
Yeah to_sentence ought to work nicely.
1 Comment
Matthew Clark
Just when I thought I couldn't be more impressed with Rails, I run into this beauty...
If you don't use Rails, try this gem of mine:
require 'joined'
puts ["apple", "banana", "pear"].joined
You will get:
apple, banana, and pear
You can also get rid of the Oxford comma:
puts ["apple", "banana", "pear"].joined(oxford: false)
"apple, banana and pear"