如何从ruby哈希中获取项目?

时间:2016-06-30 15:18:07

标签: ruby-on-rails arrays ruby hash

我从Google Drive API返回以下哈希:

@export_links=
 {"application/rtf"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=rtf",
  "application/vnd.oasis.opendocument.text"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=odt",
  "text/html"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=html",
  "application/pdf"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=pdf",
  "application/zip"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=zip",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxxx&exportFormat=docx",
  "text/plain"=>
   "https://docs.google.com/feeds/download/documents/export/Export?id=xxx&exportFormat=txt"},

我可以像这样记录导出链接:

logger.debug  file.export_links

但是,我无法获取text / plain url,尝试:

logger.debug  file.export_links["text/plain"]

对于nil:NilClass` undefined method []',这是错误的。

如何在@export_links中获取text / plain的值?

由于

1 个答案:

答案 0 :(得分:1)

你在找这个吗?

@export_links.select { |k, v| v if k == 'text/plain' }

或者

@export_links.find { |k, _| k == 'text/plain' }

如果您需要第一场比赛。