[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array in ruby

Leonard Yera

8/13/2008 6:02:00 PM

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
--
Posted via http://www.ruby-....

2 Answers

Daniel Choi

8/13/2008 6:15:00 PM

0


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

Ryan Davis

8/13/2008 6:18:00 PM

0


On Aug 13, 2008, at 11:02 , Leonard Yera wrote:

> 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">]

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]