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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:46:34+00:00 2026-05-30T15:46:34+00:00

Hash initializers: # this animals = Hash.new { [] } animals[:dogs] << :Scooby animals[:dogs]

  • 0

Hash initializers:

# this
animals = Hash.new { [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {}
# is not the same as this
animals = Hash.new { |_animals, type| _animals[type] = [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {:squirrels=>[:Rocket, :Secret], :dogs=>[:Scooby, :Scrappy, :DynoMutt]}

I saw someone post these on another question, but I don’t understand why animals appears blank in the first case. If I type

animals[:dogs]

I get the appropriate array.

  • 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-30T15:46:36+00:00Added an answer on May 30, 2026 at 3:46 pm

    The first form specifies the block that returns a default value for a key that isn’t found. That means that when you invoke animals[:dogs], there is no :dogs key in the hash, so your block gets invoked and animals[:dogs] evaluates to the result of your block, i.e. []. What happens then is that << :Scooby appends :Scooby to that empty list, which is then happily discarded.

    The second form specifies the block that, when a key is requested and isn’t found, receives as parameters the hash itself and the key that hasn’t been found. It’s a slightly more powerful version of the first constructor. The difference is in what your block does. In this second form, you modify the hash to associate [] with the key that hasn’t been found. So now it’s stored inside the hash and << :Scooby will store :Scooby there. Further calls for :dog won’t trigger the block, because now :dog exists in the hash.

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

Sidebar

Related Questions

Is this the most efficent way to forward the 'each' call to the Hash's
Today I learned this JavaScriptSerializer ser = new JavaScriptSerializer(); Foo foo = ser.Deserialize<Foo>(jsonSz); I
I've got a hash with let's say 20 values. It's initialized this way: my
I need some guidance on how to initialize this hash inside a singleton. So
I want to use this program to write a hash table to a file
config/initializers/devise.rb=> # Use this hook to configure devise mailer, warden hooks and so forth.
I have a class that inherits from Hash . When this class itself gets
SHA Hash functions
Evidently hash keys are compared in a case-sensitive manner. $ perl -e '%hash =
location.hash seems to be utterly broken in Safari 4: It can be set initially,

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.