# Array in ruby

**URL:** <https://www.ruby-forum.com/t/array-in-ruby/145765>\
**Category:** Ruby\
**Created:** [August 13, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/array-in-ruby/145765 "2008-08-13T20:05:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonard2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@leonard2](https://www.ruby-forum.com/u/leonard2)\
**Post date:** [August 13, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/array-in-ruby/145765/1 "2008-08-13T20:05:07Z")

</div>

hello list

I have this array, in irb I write @mercados  
it puts this

[[#\<Mercado id: 1, nombre: “Ibex 35”, descripcion: “”, simbolo: “^IBEX”,  
created\_at: “2008-08-13 10:22:42”, updated\_at: “2008-08-13 10:22:42”\>],  
[#\<Mercado id: 1, nombre: “Ibex 35”, descripcion: “”, simbolo: “^IBEX”,  
created\_at: “2008-08-13 10:23:39”, updated\_at: “2008-08-13 10:23:39”\>],  
[#\<Mercado id: 1, nombre: “Ibex 35”, descripcion: “”, simbolo: “^IBEX”,  
created\_at: “2008-08-13 10:23:55”, updated\_at: “2008-08-13 10:23:55”\>],  
[#\<Mercado id: 2, nombre: “Nasdaq”, descripcion: “”, simbolo: “”,  
created\_at: “2008-08-13 17:39:27”, updated\_at: “2008-08-13 17:39:27”\>]]

if I write @mercados[1] it puts

#\<Mercado id: 1, nombre: “Ibex 35”, descripcion: “”, simbolo: “^IBEX”,  
created\_at: “2008-08-13 10:23:39”, updated\_at: “2008-08-13 10:23:39”\>]

I write @mercados[i].id but it is wrong  
I want params id, How do it??

thanks

---

<div class="post-metadata">

**Author:** ![leonard2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@leonard2](https://www.ruby-forum.com/u/leonard2)\
**Post date:** [August 13, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/array-in-ruby/145765/2 "2008-08-13T20:20:26Z")

</div>

It looks like you have an array of arrays. So @mercados[1] returns and  
array containing a Mercado object. Try @mercados[1][0].id or  
@mercardos[1].first.id

---

<div class="post-metadata">

**Author:** ![leonard2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@leonard2](https://www.ruby-forum.com/u/leonard2)\
**Post date:** [August 13, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/array-in-ruby/145765/3 "2008-08-13T20:21:37Z")

</div>

On Aug 13, 2008, at 11:02 , Leonard Y. wrote:

> “^IBEX”,  
> if I write @mercados[1] it puts
> 
> #\<Mercado id: 1, nombre: “Ibex 35”, descripcion: “”, simbolo: “^IBEX”,  
> created\_at: “2008-08-13 10:23:39”, updated\_at: “2008-08-13 10:23:39”\>]

No. You’re not seeing (or writing above) the extra “[”. You have an  
array of arrays of mercados, not an array of mercados. You need to  
either flatten before you access, access 2 levels deep, or assign your  
top level array differently so you don’t have the extra nesting  
(assuming it serves no purpose).

@mercados.flatten[0] # remember, arrays are 0 indexed.

vs

@mercados[0][0]

vs

# do something different to populate @mercados 1 level deep

#mercados[0]
