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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:25:55+00:00 2026-06-05T03:25:55+00:00

Lets say I have a Hash called person whose key is name and value

  • 0

Lets say I have a Hash called person whose key is name and value is a hash having keys ‘age’ and ‘hobby’. An entry in the hash person would look like

=> person["some_guy"] = {:hobby => "biking", :age => 30}

How would I go about specifying the default for the hash ‘person’? I tried the following

=> person.default = {:hobby => "none", :age => 20}

but it doesn’t work.

EDIT:

I was setting one attribute and expecting others to be auto-populated. For eg.

=> person["dude"][:age] += 5

and this is what I was seeing

=> person["dude"]
=> {:hobby => "none", :age => 25}

which is fine. However, typing person at the prompt, I get an empty hash.

=> person
=> {}

However, what I was expecting was

=> person
=> {"dude" => {:hobby => "none", :age => 25}}
  • 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-05T03:25:57+00:00Added an answer on June 5, 2026 at 3:25 am

    Why It May Not Work for You

    You can’t call Hash#default if an object doesn’t have the method. You need to make sure that person.is_a? Hash before it will work. Are you sure you don’t have an array instead?

    It’s also worth noting that a hash default doesn’t populate anything; it’s just the value that’s returned when a key is missing. If you create a key, then you still need to populate the value.

    Why It Works for Me

    # Pass a default to the constructor method.
    person = Hash.new({hobby: "", age: 0})
    person[:foo]
    => {:hobby=>"", :age=>0}
    
    # Assign a hash literal, and then call the #default method on it.
    person = {}
    person.default = {hobby: "", age: 0}
    person[:foo]
    => {:hobby=>"", :age=>0}
    

    What You Probably Want

    Since hash defaults only apply to missing keys, not missing values, you need to take a different approach if you want to populate a hash of hashes. One approach is to pass a block to the hash constructor. For example:

    person = Hash.new {|hash,key| hash[key]={hobby: nil, age:0}}
    => {}
    
    person[:foo]
    => {:hobby=>nil, :age=>0}
    
    person[:bar]
    => {:hobby=>nil, :age=>0}
    
    person
    => {:foo=>{:hobby=>nil, :age=>0}, :bar=>{:hobby=>nil, :age=>0}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets say I have a hash a = { foo : bar ,
Lets say that I have only DHT (distributed hash table) implemented (in Python), and
Lets say I have a hash of a hash e.g. $data = { 'harry'
Lets say I have a hash which may contain hashes. params: { :action =>
I understand this is a little unorthodox. Lets say I have this hash. someHash
Lets say I have the following Perl hash: %hash = ( 'A' => {
So lets say I have an array containing a hash like this: [{head=> {title=>$20,000
Let's say I have a list of dictionaries: [ {'id': 1, 'name': 'john', 'age':
Let's say I have a hash in Ruby like this: d = {1 =>
Lets say have this immutable record type: public class Record { public Record(int x,

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.