Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:55:30+00:00 2026-06-09T03:55:30+00:00

I was curious to know more differences between [] and Array.new and {} and

  • 0

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

require 'benchmark'

many = 500000

Benchmark.bm do |b|
  b.report("[]          \t") {many.times { [].object_id }}
  b.report("Array.new   \t") { many.times { Array.new.object_id }}
  b.report("{}          \t") {many.times { {}.object_id }}
  b.report("Hash.new\t") { many.times { Hash.new.object_id }}
end


                  user     system      total        real
[]            0.080000   0.000000   0.080000 (  0.079287)
Array.new     0.180000   0.000000   0.180000 (  0.177105)
{}            0.080000   0.000000   0.080000 (  0.079467)
Hash.new      0.260000   0.000000   0.260000 (  0.264796)

I personally like to use the shorthand one’s [] and {} , the code looks so cool and readable.

Any other pointer what is the difference between them? what happens behind scene that make it so better, and suggestions if any when to use which?

I found this link but was looking to get more info.

cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:55:33+00:00Added an answer on June 9, 2026 at 3:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know any more details about google's web-crawler (aka GoogleBot)? I was curious
I am curious to know what the difference is between a cast to say
I'm curious to know more about what triggers the Reader option in Safari and
I am more curious to know what is the longest length word-series that google
Like the title says, I'm curious to know what state is contained in a
I'm curious to know how the new security model in OS X Lion will
I know the more efficient way to have a loop over array is a
So, I'd like to know which of the following is more efficient. The best
I know a little about it, was just curious to know more. FileInputStream reads
In order to create a more elegant solution I'm curios to know your suggestion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.