Question

The method

def exp_backoff up_to, url, header = {}                                                                                      
  tries = 0                                                                                                                  
  begin                                                                                                                      
    tries += 1                                                                                                               
    response = JSON.parse(open(url, header).read)                                                                            

    return response                                                                                                          
  rescue OpenURI::HTTPError => e                                                                                             
    if tries < up_to                                                                                                         
      sleep( 2 ** tries )                                                                                                    
      retry                                                                                                                  
    else                                                                                                                     
      return e                                                                                                               
    end                                                                                                                      
  end                                                                                                                        
end   

I'm calling exp_backoff expecting response to be returned, but it is not

  exp_backoff 2, status_url                                                                                                  
  session_token = response['session_token']

Getting this error

undefined local variable or method `response' for main:Object (NameError)
Was it helpful?

Solution

You didn't assign return value to anything. This will work:

response = exp_backoff 2, status_url                                                                                                  
session_token = response['session_token']

Note however that your rescue part will return different object and response['session_token'] will raise an undefined method error. Maybe you should rethink your method? Most likely, instead of returning exception object, you should simply reraise it:

rescue OpenURI::HTTPError                                                              
if tries < up_to                                                                                                         
  sleep( 2 ** tries )                                                                                                    
  retry                                                                                                                  
else                                                                                                                     
  raise                                                                                                       
end         

OTHER TIPS

The local variable response you defined within the method exp_backoff is accessible only within the method body of exp_backoff. You cannot access it from outside of that method definition.

When you assign certain value to a local variable within a method and return that, the variable assignment has no effect to outside of the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top