What is the optimal way to create an array from query result cursor?
results = @db.query("SELECT * FROM mytable")
I would like to turn results.each into an array and store it back in results.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You haven’t given much context (which db gem you’re using etc).
However, I can think of a couple of ways off the top of my head:
Bear in mind that the
#querymethod likely returns a generator for a good reason. Turning the results into an array would be a pointless memory hog (and time-consuming). Using#eachis definitely preferable as it will be more efficient.