如何使用ruby .map为数组元素添加引号

时间:2015-03-24 21:48:10

标签: sql ruby arrays sorting dictionary

请参阅下面的示例,其中我放了“某些内容”,我只是对要映射的内容感到困惑:

array_ids = ['1','2']

array = array_ids.map(something).join(',')

所以当我这样做时:

order_sql = "FIELD(ID,#{array})" 

我明白了:

order_sql = "FIELD(ID,'1','2')"

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

array_ids = ['1','2']

array = array_ids.map { |id| "'#{id}'" }.join(',')

p array
# => "'1','2'"

order_sql = "FIELD(ID,#{array})"

p order_sql
# => "FIELD(ID,'1','2')"

希望有所帮助!