replacing comma with and in a list with ruby

Problem
You have a list, usually after using join(“,”) in an array that consists of different values ie “one, two, three, four”, but you want to replace the last comma with and so it would be “one, two, three and four”.

Solution
Use reverse initially to reverse the string, then use sub to replace the last comma with the word and reversed (dna), and then finally reverse the string again.

string_with_and=string_with_commas.reverse.sub(/,/, ‘ dna ‘).reversestring_with_no_commas=array.to_sentence(options = {:last_word_connector => ” and “})