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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:56:24+00:00 2026-05-13T19:56:24+00:00

I am trying to insert a List in Hash.. However, hash[key].size is giving me

  • 0

I am trying to insert a List in Hash.. However, hash[key].size is giving me invalid results.

p = Hash.new
p = {"a" => ["b","e"]}
puts p["a"].size #prints 2 ----This is the Problem
p["a"] << ["d", "f"]
puts p["a"].size #prints 3
puts p["a"][1] #prints e
puts p["a"][2] #prints df

How, adding more lists increment the size only by one (which is correct). Is there any way to properly initialize the hash -to not increase the size to 2.

  • 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-13T19:56:24+00:00Added an answer on May 13, 2026 at 7:56 pm

    Edited to add: Your comments indicate that you want for element ‘a’ to be an array of arrays. Here it is:

    h = Hash.new
    h = {"a" => [["b","e"]]}
    p h                         # => {"a"=>[["b", "e"]]}
    h["a"] << ["d", "f"]
    p h                         # => {"a"=>[["b", "e"], ["d", "f"]]}
    

    When diagnosing a problem, prefer p to puts. puts can hide details that are important. Let’s add a few calls to p and see what we find out. Oh, and I’ll change the name of the hash from “p” to “h”. Otherwise there’s just too many “p’s” around.

    h = Hash.new
    

    The above line is unnecessary. The next line assigns an array to “h”, overwriting this assignment.

    h = {"a" => ["b","e"]}
    p h    # => {"a"=>["b", "e"]}
    p h["a"].size    # 2
    

    Everything is exactly as it should be, even h[“a”].size returning 2. why?

    p h["a"]    # => ["b", "e"]
    

    Because h[“a”] is an array with two elements.

    h["a"] << ["d", "f"]
    p h    # => {"a"=>["b", "e", ["d", "f"]]}
    

    Do you see what happened? << appends a single element to the end of an array, and that single element is the array [“d”, “f”]. And that’s why:

    p h["a"].size    # => 3
    

    Because there are indeed three elements in the array: the strings “b” and “e”, and the array [“d”, “f”].

    If you meant to append the elements “d” and “f” to the array, then do it with +=:

    h = {"a" => ["b","e"]}
    h["a"] += ["d", "f"]
    p h    # => {"a"=>["b", "e", "d", "f"]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert a new item into a sorted list, entering the item
I am trying to insert new ListItems in a Sharepoint 2010 List already created.
I'm trying to insert a String into a list. I got this error: TypeError:
Trying to insert row, and it fails to insert because of duplicate key found.
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
I'm trying to insert a bunch of data into a SharePoint list. The List
I'm trying to insert UL on every 20th sub category label. This code below
I am trying to insert a new call on my HTC programmatically. ContentValues values
I'm trying to insert a node in a linked list so that the nodes
I am trying to implement a hash table with linked list chaining. The following

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.