我有一个包含字符串值的数组:
a = ["Customer name", "Address", "Qualification"]
要求是将这些字符串值括在单引号和双引号中,如下所示:
a = ["'Customer name'", "'Address'", "'Qualification'"]
我怎样才能做到这一点?
答案 0 :(得分:2)
a = ["Customer name", "Address", "Qualification"]
a.map { |i| "'#{i}'" } # => ["'Customer name'", "'Address'", "'Qualification'"]
答案 1 :(得分:1)
有理由说你想用单引号括起每个字符串的内容,但是说你想要在它周围加上双引号是没有意义的,这是文字的一部分。但无论如何,
a.map{|s| "'#{s}'"}
# => ["'Customer name'", "'Address'", "'Qualification'"]