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

后端 未结 2 912
醉梦人生
醉梦人生 2021-02-14 18:42

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 ti

2条回答
  •  时光说笑
    2021-02-14 18:56

    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
    

提交回复
热议问题