# Hash and Arrays

**URL:** <https://www.ruby-forum.com/t/hash-and-arrays/92807>\
**Category:** Ruby\
**Created:** [April 24, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/hash-and-arrays/92807 "2007-04-24T12:46:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [April 24, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/hash-and-arrays/92807/1 "2007-04-24T12:46:12Z")

</div>

I receive from yahoo weather an Hash like this one,

response  
=\> {“xmlns:geo”=\>“[http://www.w3.org/2003/01/geo/wgs84\_pos#](http://www.w3.org/2003/01/geo/wgs84_pos#)”,  
“xmlns:yweather”=\>“[http://xml.weather.yahoo.com/ns/rss/1.0](http://xml.weather.yahoo.com/ns/rss/1.0)”,  
“version”=\>“2.0”, “channel”=\>[{“title”=\>[“Yahoo! Weather - Error”],  
“description”=\>[“Yahoo! Weather Error”], “item”=\>[{“title”=\>[“City not  
found”], “description”=\>["\n Sorry, your location ‘dfghh’ was not  
found. Please try again.\n "]}]}]}

I need to test the response for an error on channel title, I wrote :

(response.fetch(“channel”)[0]).fetch(“title”)[0]  
=\> “Yahoo! Weather - Error”

to get the string, but is there any simpler way to do it ? I’m not yet  
very easy with Hash…

thanks

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [April 24, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/hash-and-arrays/92807/2 "2007-04-24T13:18:17Z")

</div>

On Tue, Apr 24, 2007 at 07:45:04PM +0900, Josselin wrote:

> I need to test the response for an error on channel title, I wrote :
> 
> (response.fetch(“channel”)[0]).fetch(“title”)[0]  
> =\> “Yahoo! Weather - Error”
> 
> to get the string, but is there any simpler way to do it ?

response[“channel”][0][“title”][0]

or:

response[“channel”].first[“title”].first

But it also depends on how you are parsing the XML. If you’re using  
xmlsimple then you can pass option ‘ForceArray’ =\> false, which gets rid  
of  
the arrays. Then you can do

response[“channel”][“title”]

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [April 25, 2007, 9:50am UTC](https://www.ruby-forum.com/t/hash-and-arrays/92807/3 "2007-04-25T09:50:48Z")

</div>

On 2007-04-24 13:17:37 +0200, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > 
> 
> response[“channel”].first[“title”].first
> 
> But it also depends on how you are parsing the XML. If you’re using  
> xmlsimple then you can pass option ‘ForceArray’ =\> false, which gets rid of  
> the arrays. Then you can do
> 
> response[“channel”][“title”]

thanks a lot Brian, that’s exactly what I am using… so … simple !

joss
