# Accessing Array

**URL:** <https://www.ruby-forum.com/t/accessing-array/119658>\
**Category:** Ruby\
**Created:** [November 8, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/accessing-array/119658 "2007-11-08T23:19:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ernsttanaka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/46a35a/32.png) [@ernsttanaka](https://www.ruby-forum.com/u/ernsttanaka)\
**Post date:** [November 8, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/accessing-array/119658/1 "2007-11-08T23:19:17Z")

</div>

record =\> [#\<Quote:0xb189218  
@attributes={“underlying”=\>"$ADVN", “price”=\>“1579.00”,  
“date”=\>“2007-11-08 17:01:13”, “id”=\>“3362”, “impvolatility”=\>nil,  
“volume”=\>nil}\>,  
#\<Quote:0xb1877ec @attributes={“underlying”=\>"$ADVN",  
“price”=\>“1579.00”, “date”=\>“2007-11-08 16:49:15”, “id”=\>“3351”,  
“impvolatility”=\>nil, “volume”=\>nil}\>]

The above array is the result from a record.find(:all) sql call in Ruby.

I need to access attribute price of the second entry of the array.  
I have some problem coding it.

I tried record.price[1] but that seems not to be the solution.

Thanks for your help,

Ernst

---

<div class="post-metadata">

**Author:** ![ernsttanaka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/46a35a/32.png) [@ernsttanaka](https://www.ruby-forum.com/u/ernsttanaka)\
**Post date:** [November 9, 2007, 12:58am UTC](https://www.ruby-forum.com/t/accessing-array/119658/2 "2007-11-09T00:58:32Z")

</div>

Ernst T. wrote:

> record =\> [#\<Quote:0xb189218  
> @attributes={“underlying”=\>"$ADVN", “price”=\>“1579.00”,  
> “date”=\>“2007-11-08 17:01:13”, “id”=\>“3362”, “impvolatility”=\>nil,  
> “volume”=\>nil}\>,  
> #\<Quote:0xb1877ec @attributes={“underlying”=\>"$ADVN",  
> “price”=\>“1579.00”, “date”=\>“2007-11-08 16:49:15”, “id”=\>“3351”,  
> “impvolatility”=\>nil, “volume”=\>nil}\>]
> 
> The above array is the result from a record.find(:all) sql call in Ruby.
> 
> I need to access attribute price of the second entry of the array.  
> I have some problem coding it.
> 
> I tried record.price[1] but that seems not to be the solution.
> 
> Thanks for your help,
> 
> Ernst

You are very close. You called index 1 on an attribute, and you should  
call it on the array. record is your array, and you have 2 quote  
objects, so doing record[1] would return the second Quote object, and  
doing record[1].price would return 1579.00

~Jeremy

---

<div class="post-metadata">

**Author:** ![ernsttanaka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/46a35a/32.png) [@ernsttanaka](https://www.ruby-forum.com/u/ernsttanaka)\
**Post date:** [November 9, 2007, 1:55am UTC](https://www.ruby-forum.com/t/accessing-array/119658/3 "2007-11-09T01:55:12Z")

</div>

Hi Jeremy:

Thank you!

I spend some hours finding the solution in books and on the net.

Your solution worked!

I am new to Ruby programing, making great progress, only sometimes I  
have a ‘hang up’.

Thanks,

Ernst
