HOW-TO Create an Array of Hashes in Ruby
You're using a Symbol
as the index into the Hash
object that uses String
objects as keys, so simply do this:
@collection = array[0]["firstname"]
I would encourage you to use Symbol
s as Hash
keys rather than String
s because Symbol
s are cached, and therefore more efficient, so this would be a better solution:
def collection hash = { :firstname => "Mark", :lastname => "Martin", :age => 24, :gender => "M" } array = [] array.push(hash) @collection = array[0][:firstname]end