Ruby: How to loop through an object that may or may not be an array?

Posted by Shpigford on Stack Overflow See other posts from Stack Overflow or by Shpigford
Published on 2010-03-15T21:32:28Z Indexed on 2010/03/15 21:59 UTC
Read the original article Hit count: 346

Filed under:
|
|

I have an each method that is run on some user-submitted data.

Sometimes it will be an array, other times it won't be.

Example submission:

<numbers>
    <number>12345</number>
</numbers>

Another example:

<numbers>
    <number>12345</number>
    <number>09876</number>
</numbers>

I have been trying to do an each do on that, but when there is only one number I get a TypeError (Symbol as array index) error.

© Stack Overflow or respective owner

Related posts about arrays

Related posts about ruby