B
B
Bogdan2017-07-25 12:51:42
Ruby on Rails
Bogdan, 2017-07-25 12:51:42

Array.to_s and single quotes?

Hello. How can an array be convected into a string so that the values ​​are in single quotes.
Through gsub , you can then make a replacement, but maybe there is a better solution? Thanks

children_code = ['000000012','000000013','0000000147','000000015','000000016','000000017','000000018']

puts children_code.to_s 
# ["000000012", "000000013", "0000000147", "000000015", "000000016", "000000017", "000000018"]

# Вот так работает
puts children_code.to_s.gsub( '"', '\'' )
# ['000000012', '000000013', '0000000147', '000000015', '000000016', '000000017', '000000018']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2017-07-27
@2ord

It is possible like this:
or the same written differently:

puts "[%s]" % children_code.map{|cc| "'#{cc}'"}.join(',')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question