0

I need GET value from hash of hashes of Ruby on Rails

@result = {
  "destination_addresses" => [
    "Matucana 775, Santiago Centro, Región  Metropolitana de Santiago de Chile, Chile"
  ],
  "origin_addresses" => [
    "San Pablo 6134, Lo Prado, Región Metropolitana de Santiago de Chile, Chile"
  ],
  "rows" => [
    {
      "elements" => [
        {
          "distance" => { 
            "text" => "10,0 km",
            "value" => 9957
          },
          "duration" => {
            "text" => "15 min",
            "value" => 903
          },
          "status"=>"OK"
         }
       ]
     }
   ],
 "status"=>"OK"
}

@duration = @result["rows"]["elements"]["distance"]["value"]
@distance = @result["rows"]["elements"]["duration"]["value"]

This results in:

ERROR:
can't convert String into Integer

1 Answer 1

1

The problem is that rows and elements are arrays, not hashes as you expect in your query. This one should work fine:

@duration = @result["rows"][0]["elements"][0]["duration"]["value"]
@distance = @result["rows"][0]["elements"][0]["distance"]["value"]

In a nutshell, this kind of problems can be simply debugged with puts for each single [] operation. Just go on outputting values to the screen till you get any error, which would tell you which part of your hash should be explored more carefully.

puts "#{@result['rows']}"
puts "#{@result['rows'][0]}"
puts "#{@result['rows'][0]['elements']}"
# etc, till you get an error instead of the value

Assuming you are working with Google maps API, you will probably end up with some queries returning data in a different format. Sometimes you can get @result object without rows or may be a row entry without elements. Here is the way to handle this:

get_value = lambda do |api_response, inner_key|
  result = api_response['rows']
  [0, 'elements', 0, inner_key, 'value'].each do |key|
    return nil unless result
    result = result[key]
  end
  result
end

@duration = get_value.call(@result, 'duration')
@distance = get_value.call(@result, 'distance')

In case any of your @duration or @distance variables is nil at the end of this block, you can be sure that Google maps API did not parse your query correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

undefined method `[]' for nil:NilClass
That is weird. Take a look at this pastie, it contains the hash and the @duration and @distance queries returning the result. Are you sure the @result hash is completely the same as in your question and you copy-pasted the queries from the answer correctly? I have also updated my answer with an idea of how to find a failing step of the operation.
stackoverflow.com/questions/24177311/… Here is my code for a webpage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.