问题
I'm using the MySQL2 Ruby driver - but it seems a bit redundant having to call
result.each{ |r| puts r['name'] }
for a single row of data that is returned. There must be a simpler way to get the mysql field I want without having to use the each
block?
回答1:
Your result
should be a Mysql2::Result and that's Enumerable so you can use first (and the rest of the Enumerable
goodies) on it:
puts result.first['name']
来源:https://stackoverflow.com/questions/14064649/ruby-mysql-fetching-single-row-but-still-using-each