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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:43:32+00:00 2026-06-08T01:43:32+00:00

I tried the following ruby code, which I thought would return a hash of

  • 0

I tried the following ruby code, which I thought would return a hash of word lengths to the words with those lengths. Instead it is empty.

map = Hash.new(Array.new)    
strings = ["abc","def","four","five"]
strings.each do |word|
  map[word.length] << word  
end   

However, if I modify it to

map = Hash.new
strings = ["abc","def","four","five"]
strings.each do |word|
  map[word.length] ||= []
  map[word.length] << word  
end

It does work.

Doesn’t the first version just create a hash whose default values are an empty array? In this case, I don’t understand why the 2 blocks give different values.

  • 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-08T01:43:33+00:00Added an answer on June 8, 2026 at 1:43 am

    The problem is that you aren’t actually assigning anything to the hash keys, you’re just using the << operator to modify the existing contents of a default value. Since you don’t assign anything to the hash key, it is not added. In fact, you’ll notice the default value is the one modified:

    h = Hash.new []
    p h[0]           # []
    h[0] << "Hello"
    p h              # {}
    p h[0]           # ["Hello"]
    p h[1]           # ["Hello"]
    

    This is because the same Array object is maintained as the default value. You can fix it by using the + operator, though it may be less efficient:

    map = Hash.new []
    strings = ["abc", "def", "four", "five"]
    
    strings.each do |word|
        map[word.length] += [word]
    end
    

    And now it works as expected.

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

Sidebar

Related Questions

I tried following the README file in Ruby 1.9.1 but I can't compile it
I've been following the Ruby On Rails Tutorial and tried to get rid of
I tried following some of the code from here and here , but I
Today I tried the following snippets of code and I don't understand why I
I'm trying to implement the following code in Ruby and getting stuck on it.
I am using Ruby 1.8.6 for the following code: # Create an array and
I wrote the following Ruby code: def myItems if @item1 yield @item1 end if
In the following ruby code, the output becomes: x y x = x %s
I tried the following code in both facebook's phpsh and the standard crappy php
I have the following code. I'm still a newbie in Ruby on Rails. As

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.