Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby: wrap each element of an array in additional quotes

I have a following string :

a = "001;Barbara;122"

I split in into array of strings:

names = a.split(";")
names = ["001", "Barbara", "122"] 

What should I do to have each element wrapped additionally in '' quotes? The result should be

names = ["'001'", "'Barbara'", "'122'"]

I know it sounds strange but I need it for database query in ruby on rails. For some reason I cannot access database record if my name is in "" quotes. I do have mk1==0006 in the database but rails does not want to access it somehow. However, it does access 1222.

sql = "SELECT mk1, mk2, pk1, pk2, pk3, value_string, value_number FROM infos WHERE mk1 in (0006) AND value_string ='männlich';"         
recs = ClinicdbInfo.find_by_sql(sql)     
=> [] 

sql = "SELECT mk1, mk2, pk1, pk2, pk3, value_string, value_number FROM infos WHERE mk1 in (1222) AND value_string ='männlich';"         
recs = ClinicdbInfo.find_by_sql(sql)     
 => [#<Info mk1: "1222", mk2: "", pk1: "Information allgemein", pk2: "Geschlecht", pk3: "Wert", value_string: "männlich", value_number: nil>] 

So, I just need to wrap every element of names into additional ''-quotes.

like image 749
Alina Avatar asked May 16 '15 14:05

Alina


1 Answers

names.map{ |e| "'" + e + "'" }
=> ["'001'", "'Barbara'", "'122'"]

or

names.map{ |e| "'#{e}'" }
=> ["'001'", "'Barbara'", "'122'"]
like image 128
pangpang Avatar answered Oct 06 '22 12:10

pangpang