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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:38:56+00:00 2026-05-31T01:38:56+00:00

See my Ruby code: h=Hash.new([]) h[0]=:word1 h[1]=h[1]<<:word2 h[2]=h[2]<<:word3 print \nHash = print h Output

  • 0

See my Ruby code:

h=Hash.new([])
h[0]=:word1    
h[1]=h[1]<<:word2
h[2]=h[2]<<:word3
print "\nHash = "
print h

Output is:

Hash = {0=>:word1, 1=>[:word2, :word3], 2=>[:word2, :word3]}

I’ve expected to have

Hash = {0=>:word1, 1=>[:word2], 2=>[:word3]}

Why the second hash element(array) was appended?

How can I append just the 3rd hash’s element with new array element?

  • 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-31T01:38:57+00:00Added an answer on May 31, 2026 at 1:38 am

    If you supply a single value as a parameter to Hash.new (e.g. Hash.new( [] ) that exact same object is used as the default value for every missing key. That’s what you had, and that’s what you don’t want.

    You could instead use the block form of Hash.new, where the block is invoked each time a missing key is requested:

    irb(main):001:0> h = Hash.new{ [] }
    #=> {}
    irb(main):002:0> h[0] = :foo
    #=> :foo
    irb(main):003:0> h[1] << :bar
    #=> [:bar]
    irb(main):004:0> h
    #=> {0=>:foo}
    

    However, as we see above you get the value but it’s not set as the value for that missing key. As you did above, you need to hard-set the value. Often, what you want instead is for the value to magically spring to life as the value of that key:

    irb(main):005:0> h = Hash.new{ |h,k| h[k] = [] }
    #=> {}
    irb(main):006:0> h[0] = :foo
    #=> :foo
    irb(main):007:0> h[1] << :bar
    #=> [:bar]
    irb(main):008:0> h
    #=> {0=>:foo, 1=>[:bar]}
    

    Now not only do we get the brand new array back when we ask for a missing key, but this automatically populates our hash with the array so that you don’t need to do h[1] = h[1] << :bar

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

Sidebar

Related Questions

I have the following ruby code: class Mp def initialize Test.new.mytest Work.new.mywork ha address
I'm reading some ruby code, and see 0..size and 0...size are used in similar
I have a ruby on rails application and it would be nice to see
I have two Ruby arrays, and I need to see if they have any
see fiddle Firstly i am new to jquery. i have to make one cell
Whenever I see Ruby code that says: arrayNames.collect { ... } I forget what
Im studying some ruby code and I see this varx variable being used with
In my ruby code , I try to put all the output messages in
I have the Ruby code below, I am using windows API calls and I
I am very new to Ruby, and programming in general. Firstly, I have the

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.