Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array or hash in Ruby

Tags:

arrays

ruby

hash

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
like image 770
Sergey Chechaev Avatar asked Jun 20 '13 10:06

Sergey Chechaev


2 Answers

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.

like image 101
mikdiet Avatar answered Sep 21 '22 13:09

mikdiet


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

like image 37
Arup Rakshit Avatar answered Sep 23 '22 13:09

Arup Rakshit