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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:58:26+00:00 2026-05-30T23:58:26+00:00

Ruby lets you define default values for hashes: h=Hash.new([‘alright’]) h[‘meh’] # => [alright] Assignment

  • 0

Ruby lets you define default values for hashes:

h=Hash.new(['alright'])
h['meh'] # => ["alright"]

Assignment of a value shows up when displaying the hash, but a modified default does not. Where’s 'bad'?

h['good']=['fine','dandy']
h['bad'].push('unhappy')
h # => {"good"=>["fine", "dandy"]}

'bad' shows up if we explicitly ask.

h['bad'] # => ["alright", "unhappy"]

Why does the modified default value not show up when displaying the hash?

  • 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-30T23:58:27+00:00Added an answer on May 30, 2026 at 11:58 pm

    Hash’s default value doesn’t work like you’re expecting it to. When you say h[k], the process goes like this:

    1. If we have a k key, return its value.
    2. If we have a default value for the Hash, return that default value.
    3. If we have a block for providing default values, execute the block and return its return value.

    Note that (2) and (3) say nothing at all about inserting k into the Hash. The default value essentially turns h[k] into this:

    h.has_key?(k) ? h[k] : the_default_value
    

    So simply accessing a non-existant key and getting the default value back won’t add the missing key to the Hash.

    Furthermore, anything of the form:

    Hash.new([ ... ])
    # or
    Hash.new({ ... })
    

    is almost always a mistake as you’ll be sharing exactly the same default Array or Hash for for all default values. For example, if you do this:

    h = Hash.new(['a'])
    h[:k].push('b')
    

    Then h[:i], h[:j], … will all return ['a', 'b'] and that’s rarely what you want.

    I think you’re looking for the block form of the default value:

    h = Hash.new { |h, k| h[k] = [ 'alright' ] }
    

    That will do two things:

    1. Accessing a non-existent key will add that key to the Hash and it will have the provided Array as its value.
    2. All of the default values will be distinct objects so altering one will not alter the rest.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to ruby on rails. Lets say i have this text.. Ashley
Let's say I have a hash in Ruby like this: d = {1 =>
Lets say I have the following ruby definition at the topmost level callable =
So, lets say I have two nearly identical classes in C# and Ruby: C#
Possible Duplicate: How to sum array members in Ruby? Lets say I have this
I am attempting to do some calculations on a simple ruby screen. Lets say
Using Ruby (1.8.7) and Rails (2.3.8) doing myObject.attributes gives you a hash of attribute
First off my background: I'm new to Java coming over from Ruby. If that
I am new to Ruby and need help in accessing a function which is
How can I force the Time.rfc2822 function to spit out +0000 ? Ruby lets

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.