Ruby / MySQL fetching single row but still using .each?

不问归期 提交于 2019-12-10 14:29:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!