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 6833227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:10+00:00 2026-05-26T22:57:10+00:00

Are these methods of creating an empty Ruby Hash different? If so how? myHash

  • 0

Are these methods of creating an empty Ruby Hash different? If so how?

myHash = Hash.new

myHash = {}

I’d just like a solid understanding of memory management in Ruby.

  • 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-05-26T22:57:10+00:00Added an answer on May 26, 2026 at 10:57 pm

    There are many ways you can create a Hash object in Ruby, though the end result is the same sort of object:

    hash = { }
    hash = Hash.new
    hash = Hash[]
    hash = some_object.to_h
    hash = YAML.load("--- {}\n\n")
    

    As far as memory considerations go, an empty Hash is significantly smaller than one with even a singular value in it. Arrays tend to be smaller than Hashes at small sizes, but will be more efficient at larger scales.

    In practice, though, the important thing to remember in Ruby is that every time you create an object it costs you something, even if it’s only an infinitesimal amount. These little hits add up if you’re needlessly creating billions of objects.

    Generally you should avoid creating structures that will not be used, and instead create them on demand if that wouldn’t complicate things needlessly. For example, a typical pattern is:

    def cache
      @cache ||= { }
    end
    

    Until this method is called, the cache Hash is never defined. The memory savings in this instance is nearly insignificant, but if that was loading a large configuration file or importing several hundred MB of data from a database you can imagine the savings would be significant in those instances where that data is not exercised.

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

Sidebar

Related Questions

I was just wondering if there are any methods of creating nice, smooth transition
From my understanding, each of these methods: get() and put() are atomic. But, when
I'm having troubles understanding why save and create should be any different using these
I understand that these methods are for pickling/unpickling and have no relation to the
Of all these methods what's being run and in what order?? I guess the
I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo)
What is the difference, if any, between these methods of indexing into a PHP
These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f =>
These two methods appear to behave the same to me public IEnumerable<string> GetNothing() {
how can I prevent file_get_contents from creating an empty file when being used as

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.