Sergey Chechaev
Sergey Chechaev

Reputation: 570

Array or hash in Ruby

Could anyone explain what is faster for reading, writing in Ruby: an Array or a Hash? And what are the usecases for Array and Hash?

Array.new
Hash.new

Upvotes: 5

Views: 176

Answers (2)

mikdiet
mikdiet

Reputation: 10018

If you need to store only some unique unordered values, please consider using Set.new. It's convenient hash based class with writing and reading in constant time.

Upvotes: 3

Arup Rakshit
Arup Rakshit

Reputation: 118271

For reading and writing Hash.new is faster than Array.new. Watch this Why Hashes Will Be Faster in Ruby 2.0.

Upvotes: 2

Related Questions