S
Saeed Bhuta
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.
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.