使用Ruby 1.9。
我有一个数组override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first!
if touch.view == profileImage {
println("image touched")
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first!
if touch.view == profileImage {
println("image released")
}
}
我需要将其转换为格式[1,2,3]
,以便在SQL查询(IN语句)中应用它,数据库是MySQL。请提出一些好的解决方案。
谢谢:)
答案 0 :(得分:2)
看看上面的评论,不确定你是否仍然想要这样做,但只是为了好玩:
"('#{ [1,2,3].map(&:to_s).join("\',\'") }')"
#=> "('1','2','3')"
更新:根据@tadman的评论
假设这里的SQL实现是一些伪代码:
irb(main):003:0> array = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):004:0> array.map{|id| "$#{id}"}.join(",")
=> "$1,$2,$3,$4"
irb(main):011:0> ["SELECT * FROM table WHERE id IN (#{array.map{|id| "$#{id}" }.join(',')})", array]
=> ["SELECT * FROM table WHERE id IN ($1,$2,$3,$4)", [1, 2, 3, 4]]