Ruby on rails 如何从Ruby哈希中获取项目?
我从Google Drive API返回了以下哈希:Ruby on rails 如何从Ruby哈希中获取项目?,ruby-on-rails,arrays,ruby,hash,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/dow
@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
但是,我无法获取文本/普通url,请尝试:
logger.debug file.export_links["text/plain"]
对于nil:NilClass`,undefined方法
[]”出现错误
如何获取@export\u链接中的text/plain值 你在找这个吗
@export_links.select { |k, v| v if k == 'text/plain' }
或
如果您需要第一个匹配项。什么是
文件.export\u links.class
?@spickermann返回哈希或NilClass第一个想法返回私有方法
为nil:NilClass选择'called for nil:NilClass`第二个未定义的方法
find'您的代码示例显示了对@export\u links
的赋值。它应该包含散列,但是当你尝试这个想法时它是零。
@export_links.find { |k, _| k == 'text/plain' }