How to improve code that quotes all array elements with `'` and returns a string containing all those quoted and comma-separated elements?

arrays, collect, ruby, ruby-on-rails

Solution

I use

"'#{%w{a b c}.join("', '")}'"

Here is expanded version:

' # Starting quote
%w{a b c}.join("', '") # Join array with ', ' delimiter that would give a', 'b', 'c
' # Closing quote

Problem

I am using Rails 3.2.2 and I would like to quote all array elements with `'` and return a string containing all those quoted and comma-separated elements. At this time I am using ``` ['a', 'b', 'c'].collect {|x| "'#{x}'"}.join(", ") # => "'a', 'b', 'c'" ``` but I think I can improve the above code (maybe by using a unknown to me Ruby method, if it exists). Is it possible?

Original source

Related problems