52

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 2

102

Yeah to_sentence ought to work nicely.

http://apidock.com/rails/Array/to_sentence

Sign up to request clarification or add additional context in comments.

1 Comment

Just when I thought I couldn't be more impressed with Rails, I run into this beauty...
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.