Is it possible to concatenate Array ['a', 'b', 'c'] to String "a, b and c" ?
But ['a', 'b'] should transform to "a and b".
Rails provides a to_sentence helper:
> ['a', 'b'].to_sentence
=> "a and b"
> ['a', 'b', 'c'].to_sentence
=> "a, b, and c"
If you want a, b and c rather than a, b, and c you can change the last_word_connector:
> ['a', 'b', 'c'].to_sentence(last_word_connector: " and ")
=> "a, b and c"
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