Differences between literals and constructors? ([] vs Array.new and {} vs Hash.new)

前端 未结 3 1081
遥遥无期
遥遥无期 2021-02-05 05:39

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



        
3条回答
  •  别那么骄傲
    2021-02-05 06:15

    With Hash.new you can set the default value of the hash for unset keys. This is quite useful if you're doing statistics, because Hash.new(0) will let you increment keys without explicitly initializing them.

提交回复
热议问题