# Array Confusion

**URL:** <https://discuss.rubyonrails.org/t/array-confusion/68496>\
**Category:** rubyonrails-talk\
**Created:** [February 6, 2014, 2:53am UTC](https://discuss.rubyonrails.org/t/array-confusion/68496 "2014-02-06T02:53:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BeeRich](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@BeeRich](https://discuss.rubyonrails.org/u/BeeRich)\
**Post date:** [February 6, 2014, 2:53am UTC](https://discuss.rubyonrails.org/t/array-confusion/68496/1 "2014-02-06T02:53:28Z")

</div>

I have the following instruction that returns an array that I’m quite confused about. I can’t even read it. Can someone shed some light as to how to reach into this array of hashes (that’s what I can see) to get the location lat and lon?

res = Geocoder.search(addy1)

This is the res:

[#\<Geocoder::Result::Google:0x007fb46c345e18 @data={"address\_components"=\>[{"long\_name"=\>"1600", "short\_name"=\>"1600", "types"=\>["street\_number"]}, {"long\_name"=\>"Amphitheatre Parkway", "short\_name"=\>"Amphitheatre Pkwy", "types"=\>["route"]}, {"long\_name"=\>"Mountain View", "short\_name"=\>"Mountain View", "types"=\>["locality", "political"]}, {"long\_name"=\>"California", "short\_name"=\>"CA", "types"=\>["administrative\_area\_level\_1", "political"]}, {"long\_name"=\>"United States", "short\_name"=\>"US", "types"=\>["country", "political"]}, {"long\_name"=\>"94043", "short\_name"=\>"94043", "types"=\>["postal\_code"]}], "formatted\_address"=\>"1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", "geometry"=\>{"location"=\>{"lat"=\>37.4219985, "lng"=\>-122.0839544}, "location\_type"=\>"ROOFTOP", "viewport"=\>{"northeast"=\>{"lat"=\>37.4233474802915, "lng"=\>-122.0826054197085}, "southwest"=\>{"lat"=\>37.4206495197085, "lng"=\>-122.0853033802915}}}, "types"=\>["street\_address"]}, @cache\_hit=nil\>]

Outside of the conversion of string to integer errors (?), I can’t traverse this array because it holds a comment in the first line there, before an instance variable is set to the data. Quite confused. Any insight appreciated.

Cheers

---

<div class="post-metadata">

**Author:** ![Gautam\_Rege1](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@Gautam\_Rege1](https://discuss.rubyonrails.org/u/Gautam_Rege1)\
**Post date:** [February 6, 2014, 5:06am UTC](https://discuss.rubyonrails.org/t/array-confusion/68496/2 "2014-02-06T05:06:41Z")

</div>

Your’e looking at the result in correctly,— that is not a comment in the array! Its the “stringified” output of the array that contains Geocoder::Result objects.

res = Geocoder.search(addy1).first

res.data # =\> gives you entire data .. seen as @data and then you can traverse as a hash

There are other helper routines too (a few mentioned below). res.address res.city res.state res.formatted\_address res.coordinates

etc. See these two files to learn all the methods of Geocoder::Result

> <https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/results/google.rb>

> <https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/results/base.rb>

Cheers! @gautamrege

---

<div class="post-metadata">

**Author:** ![BeeRich](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@BeeRich](https://discuss.rubyonrails.org/u/BeeRich)\
**Post date:** [February 6, 2014, 5:45am UTC](https://discuss.rubyonrails.org/t/array-confusion/68496/3 "2014-02-06T05:45:40Z")

</div>

I see. Now it is clear. I was under the impression that the inspect method was just a representation of the returned array. Good to know. Thanks for the notes.

Cheers
