# Referring to Hash and Array Elements

**URL:** <https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056>\
**Category:** Ruby\
**Created:** [February 24, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056 "2010-02-24T12:12:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [February 24, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056/1 "2010-02-24T12:12:06Z")

</div>

Hi All,

I am a beginner to Ruby. Currently I am working with Hashes and Arrays.

I have a loop where the code saves several objects into a Hash.  
Everytime the loop is run the objects in the Hash are overwritten,  
therefore I append the objects into an Array so that data is not lost.

#begining of loop ommitted

```
            trainHash = {}
            trainHash['order'] = tubeOrder
            trainHash['destination'] = destination
            trainHash['currentLocation'] = currentLocation
            trainHash['dueIn'] = dueInMinsValue
            #trainHash['dueintimestamp'] = dueInTimestamp.to_s

            trainArray << trainHash
         end

```

From what I have read so far, Arrays are indexed by an integer with  
indexing starting at ‘0’.

So for example, if the above loop was run twice; the number of objects  
in the trainArray would be two and referred using [0] for the first  
object and [1] for the second object.

My question is, is it possible to refer the elements as they were stored  
in the Hash? For example, I want the code to return the value of the  
‘order’ field in the second object in the trainArray.

If not, is it possible to dynamically generate a key for the hash  
everytime the loop is run so that the objects are not overwritten in the  
hash?

I would sincerely appreciate any help.

Saeed.

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [February 24, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056/2 "2010-02-24T13:04:24Z")

</div>

2010/2/24 Saeed B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> trainHash = {}
> 
> ```
> 
> indexing starting at ‘0’.
> 
> So for example, if the above loop was run twice; the number of objects  
> in the trainArray would be two and referred using [0] for the first  
> object and [1] for the second object.
> 
> My question is, is it possible to refer the elements as they were stored  
> in the Hash?

I am not sure I understand that question.

> For example, I want the code to return the value of the  
> ‘order’ field in the second object in the trainArray.

Like this?

second\_order = train\_array[1][‘order’]

> If not, is it possible to dynamically generate a key for the hash  
> everytime the loop is run so that the objects are not overwritten in the  
> hash?

Yes, of course. There are also other options. It depends how you  
want to further process the data. To me it looks like all values in  
the Hash belong to a single record. In this case I would stick with  
the two level approach, i.e. have an Array of records (Hashes or other  
types).

If you just want to collect all individual values per key, you can use  
Arrays as Hash values, e.g.

train\_hash = Hash.new {|h,k| h[k] = []}

# loop

```
           train_hash['order'] << tube_order
           train_hash['destination'] << destination
           train_hash['currentLocation'] << current_location
           train_hash['dueIn'] << due_in_mins_value
           #train_hash['dueintimestamp'] << due_in_timestamp.to_s

```

# end loop

Btw, I changed names to match the usual convention in Ruby.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [February 24, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056/3 "2010-02-24T13:11:44Z")

</div>

> > For example, I want the code to return the value of the  
> > ‘order’ field in the second object in the trainArray.
> 
> Like this?
> 
> second\_order = train\_array[1][‘order’]

Awesome! This is the syntax that I needed, thanks Robert.

---

<div class="post-metadata">

**Author:** ![saeed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@saeed](https://www.ruby-forum.com/u/saeed)\
**Post date:** [February 24, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/referring-to-hash-and-array-elements/184056/4 "2010-02-24T15:17:17Z")

</div>

On Wed, Feb 24, 2010 at 6:12 AM, Saeed B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> trainHash = {}
> 
> ```
> 
> indexing starting at ‘0’.
> 
> So for example, if the above loop was run twice; the number of objects  
> in the trainArray would be two and referred using [0] for the first  
> object and [1] for the second object.
> 
> My question is, is it possible to refer the elements as they were stored  
> in the Hash? For example, I want the code to return the value of the  
> ‘order’ field in the second object in the trainArray.

Robert already answered this question. I’d like to point out that  
your code for building the hashes inserted into the array could be  
simplified:

#begining of loop ommitted

```
 trainArray << { 'order' => tubeOrder, 'destination' =>

```

destination, ‘currentLocation’ =\> currentLocation,  
‘dueIn’ =\> dueInMinsValue,  
‘dueintimestamp’ =\> dueInTimestamp.to\_s}

end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)
