Ruby / MySQL extracts a single row but still uses .each?

I am using MySQL2 Ruby Driver - but it seems a little redundant to call

result.each{ |r| puts r['name'] } 

for one row of returned data. Should there be an easier way to get the mysql field I want without using each block?

+4
source share
1 answer

Your result should be Mysql2::Result and that is Enumerable so that you can use first (and the rest of the positive Enumerable properties):

 puts result.first['name'] 
+15
source

All Articles