Search code examples
rubyloopsrescue

Ruby - "Do" loop and "rescue"


I'm using the Microsoft computer vision API. The API can recognise faces and gives data on how many people are in an image, what estimated age they are, and what estimated gender. However, I have a "do" loop which I can't "rescue." Here's the code below:

 values = json_data['faces'].map do |result| 

Here's the error I receive:

C:/Users/KVadher/Desktop/micr1.rb:122:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)

I want my code to look something like this:

 begin
  values = json_data['faces'].map do |result| 
 rescue
 end

However, when I do this, I get the following error:

C:/Users/USERNAME/Desktop/micr1.rb:123: syntax error, unexpected keyword_rescue

How do I pass my code if a request doesn't apply to it?


Solution

  • You map block should have end

    begin
      values = json_data['faces'].map do |result|
        # ...
      end
    rescue
    end