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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:26:35+00:00 2026-05-28T19:26:35+00:00

Learning Ruby. Needed to create a hash of arrays. This works… but I don’t

  • 0

Learning Ruby. Needed to create a hash of arrays. This works… but I don’t quite understand what Ruby is doing. Could someone explain it in detail?

months = Hash.new{|h, k| h[k] = []}
  • 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-28T19:26:37+00:00Added an answer on May 28, 2026 at 7:26 pm

    This uses the Hash.new constructor to create a hash whose items default to the empty list. You can therefore do something like this:

    months[2012] << 'January'
    

    and the array will be created without you doing a months[2012] = [] first.

    Short explanation: { |h, k| h[k] = [] } Is a Ruby block and as mu has mentioned, it can to some degree be compared to function in Javascript or lambda in Python. It basically is an anonymous piece of code that takes two arguments (h, k, the pipes only have the meaning to separate parameters from code) and returns a value. In this case it takes a hash and a key and sets the value of the key to a new array []. It returns a reference to the hash (but that’s not important in this context).

    Now to Hash.new: It is the constructor for a Hash, of which I assume you already now what it is. It optionally takes a block as an argument that is called whenever a key is accessed that does not yet exist in the Hash. In the above example, the key 2012 was not accessed before, so the block is called, which creates a new array and assigns it to the key. Afterwards, the << operator can work with that array instance.

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

Sidebar

Related Questions

I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I'm learning Ruby on Rails and doing the contactlist thing, 20 minute blog and
I'm learning Ruby on Rails. I'm using some associations (has_many :through, ...) But i'm
I am learning Ruby and have to do some homework, but have problem with
I am learning Ruby on Rails and i did this scaffolding command: rails g
I'm learning Ruby and have just gotten into some stuff about arrays and ranges.
I've been slowly learning Ruby (at this point, maybe the first language I've invested
I'm just learning Ruby so apologies if this is too newbie for around here,
I'm just learning ruby and trying to understand the scope of code executed in
I am learning Ruby on Rails so I'm sure I'll find this out sooner

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.