# Class and Hash.from\_xml

**URL:** <https://www.ruby-forum.com/t/class-and-hash-from-xml/120575>\
**Category:** Rails\
**Created:** [November 18, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/class-and-hash-from-xml/120575 "2007-11-18T19:54:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [November 18, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/class-and-hash-from-xml/120575/1 "2007-11-18T19:54:00Z")

</div>

I have some xml that looks like this:

 Smith Jones

In this case,

acct\_hash = Hash.from\_xml

produces

acct\_hash[‘accounts’][‘account’]

which is an Array. But if there is only one account in the xml, as in

 Smith 

then acct\_hash[‘accounts’][‘account’] is a Hash.

So to turn those into Account objects I have to do slightly different  
things depending on the number of accounts.  
To figure out what to do I’m using

if acct\_hash[‘accounts’][‘account’].class.to\_s == “Array”  
…

which works, but seems ‘wrong.’ Is there a better way?  
I wonder why Hash.from\_xml doesn’t just always create an Array. I’m  
sure there’s a good reason, but it would be simpler in this case.

Thanks,  
-George

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [November 18, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/class-and-hash-from-xml/120575/2 "2007-11-18T20:50:58Z")

</div>

George B. wrote:

> I have some xml that looks like this:
> 
> Smith Jones
> 
> In this case,
> 
> acct\_hash = Hash.from\_xml
> 
> produces
> 
> acct\_hash[‘accounts’][‘account’]
> 
> which is an Array. But if there is only one account in the xml, as in
> 
> Smith 
> 
> then acct\_hash[‘accounts’][‘account’] is a Hash.
> 
> So to turn those into Account objects I have to do slightly different  
> things depending on the number of accounts.  
> To figure out what to do I’m using
> 
> if acct\_hash[‘accounts’][‘account’].class.to\_s == “Array”  
> …
> 
> which works, but seems ‘wrong.’ Is there a better way?  
> I wonder why Hash.from\_xml doesn’t just always create an Array. I’m  
> sure there’s a good reason, but it would be simpler in this case.

Yep, that’s annoying and wrong, but it was fixed months ago in edge:  
[http://dev.rubyonrails.org/changeset/7074](http://dev.rubyonrails.org/changeset/7074)

If you upgrade to edge or Rails 2.0 RC1, things will work as you want.  
Note that you need the type=“array” attribute in the array entity, like  
so:

 Smith 

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [November 18, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/class-and-hash-from-xml/120575/3 "2007-11-18T22:13:34Z")

</div>

On Nov 18, 2007, at 1:50 PM, Josh S. wrote:

> Yep, that’s annoying and wrong, but it was fixed months ago in edge:  
> [http://dev.rubyonrails.org/changeset/7074](http://dev.rubyonrails.org/changeset/7074)
> 
> If you upgrade to edge or Rails 2.0 RC1, things will work as you want.

Thanks John. Upgrading isn’t an option right now, but the way I wrote  
it, it turns out, should work in either case.

> Note that you need the type=“array” attribute in the array entity,  
> like  
> so:
> 
> Smith

I’m getting the xml by doing a accts = Account.find(:all…), then  
accts.to\_xml.  
So Array.to\_xml is not adding that type=“array” part. I guess that’s  
also in edge.

-George

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [November 19, 2007, 12:19am UTC](https://www.ruby-forum.com/t/class-and-hash-from-xml/120575/4 "2007-11-19T00:19:33Z")

</div>

On Nov 18, 2007, at 1:50 PM, Josh S. wrote:

> Yep, that’s annoying and wrong, but it was fixed months ago in edge:  
> [http://dev.rubyonrails.org/changeset/7074](http://dev.rubyonrails.org/changeset/7074)

I see you were the one who filed the ticket. btw, don’t know why I  
wrote John instead of Josh earlier, I wasn’t thinking it, it just came  
out 🙂
