Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails method for concatenate

Is it possible to concatenate Array ['a', 'b', 'c'] to String "a, b and c" ?
But ['a', 'b'] should transform to "a and b".

like image 224
ibylich Avatar asked Mar 19 '26 17:03

ibylich


1 Answers

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" 
like image 114
pjumble Avatar answered Mar 21 '26 09:03

pjumble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!