AnApprentice AnApprentice - 1 year ago 81
Ruby Question

How to get items from a ruby hash?

I have the following hash being returned from the Google Drive API:


I'm able to log export links like so:

logger.debug file.export_links

However, I'm unable to get the text/plain url, trying:

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

This is erroring with
undefined method
[]' for nil:NilClass`.

How can I get the value for text/plain within @export_links?


Answer Source

Are you looking for this? { |k, v| v if k == 'text/plain' }


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

if you need the first match.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download