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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:48:42+00:00 2026-05-21T11:48:42+00:00

I have executed the following in irb: irb(main):068:0* map = Hash.new(Array.new) => {} irb(main):069:0>

  • 0

I have executed the following in irb:

irb(main):068:0* map = Hash.new(Array.new)
=> {}
irb(main):069:0> map["a"]
=> []
irb(main):070:0> map["a"].push("hello")
=> ["hello"]
irb(main):071:0> map["a"].push(1)
=> ["hello", 1]
irb(main):072:0> map.has_key?("a")
=> false
irb(main):073:0> map.keys
=> []
irb(main):074:0>

Why is it that once i have added key "a" to the hash it is not appearing in the result of Hash#keys?

Thanks

  • 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-21T11:48:43+00:00Added an answer on May 21, 2026 at 11:48 am

    The Problem

    By calling

    map["a"].push("hello")
    

    you alter the Hash’s default object. In fact, after that, every possible key will deliver "hello", but the key isn’t really initialized. The hash will only know its default object, but you didn’t tell it to "initialize" the key.

    ruby-1.9.2-head :002 > map["a"].push("Hello")
     => ["Hello"] 
    ruby-1.9.2-head :003 > map["a"]
     => ["Hello"] 
    ruby-1.9.2-head :004 > map["b"]
     => ["Hello"] 
    ruby-1.9.2-head :004 > map.keys
     => [] 
    

    What you might want to do is specifically initialize the key:

    ruby-1.9.2-head :008 > map["a"] = Array.new
     => [] 
    ruby-1.9.2-head :009 > map.keys
     => ["a"]
    

    But this isn’t really what you want.

    Solution:

    This default behavior can be achieved by using the following method to initialize the hash:

    map = Hash.new { |hash, key| hash[key] = Array.new }
    

    For example:

    ruby-1.9.2-head :010 > map = Hash.new { |hash, key| hash[key] = Array.new }
     => {} 
    ruby-1.9.2-head :011 > map["a"]
     => [] 
    ruby-1.9.2-head :012 > map["b"]
     => [] 
    ruby-1.9.2-head :013 > map.keys
     => ["a", "b"] 
    

    (I’m no Ruby expert, so if there are some suggestions please add a comment)

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

Sidebar

Related Questions

I have the following loop inside an function init(); which is executed onload, I
I have used the following statements to get the current time. print $query executed
I have the following query that when executed, it just runs for hours and
I have executed the following command git add <foo.java> git commit -m add the
I have executed the following create statement using SQLWorkbench at my target postgresql database:
i have executed the following code public class Activity_Threads_Handler extends Activity { private int
I have successful executed following commands: tar xzvf fcgi-2.4.0.tar.gz cd fcgi-2.4.0 ./configure --prefix=/usr/local/fcgi make
I have to execute the following query in Android SQLite UPDATE player SET rank=rank+1
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have tomcat 6.0 with 2 webapps. One of them executes the following code:

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.