I have an array,
array = [1,2,3]
Need to transform it to:
newArray = [{id: 1}, {id: 2}, {id: 3}]
I know this, Is there any efficient way?
array.each { |id| newArray << { id: id } }
A very elegant way:
additionally you could use Base58 random key and hash it twice to mitigate timing attacks
JSON.parse([1,2,3].map(&:to_s).collect{ "{\"id\": \"#{_1}\"}" }.join(",").prepend("[").concat("]")).collect(&:symbolize_keys)
=> [{:id=>"1"}, {:id=>"2"}, {:id=>"3"}]