Ruby/MySQL获取单行,但仍使用.each? [英] Ruby / MySQL fetching single row but still using .each?

查看:73
本文介绍了Ruby/MySQL获取单行,但仍使用.each?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MySQL2 Ruby驱动程序-但似乎不得不调用 >

I'm using the MySQL2 Ruby driver - but it seems a bit redundant having to call

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

对于返回的单行数据.必须有一种更简单的方法来获得我想要的mysql字段,而不必使用each块?

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?

推荐答案

您的result应该是 Enumerable 可以使用 first (其余的Enumerable好东西):

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']

这篇关于Ruby/MySQL获取单行,但仍使用.each?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆