I was curious to know more differences between [] and Array.new and {} and Hash.new
I ran same benchmarks on it and seems like the shorthands are winners
Robert already mentioned the default value of the Hash.new
You may also use compley 'default'-values with the block variant of Hash.new
:
x = Hash.new { |hash, key|
hash[key] = key * 2
}
p x #-> {}
p x[1] #-> 2
p x #-> {1=>2}
Array.new
can also be used to get default values:
p Array.new(5, :a) #-> [:a, :a, :a, :a, :a]