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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:46:58+00:00 2026-05-23T16:46:58+00:00

I will just paste down a simple example i tried so that it would

  • 0

I will just paste down a simple example i tried so that it would be clear to those who read this.

irb(main):001:0> h =  { }
=> {}
irb(main):002:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):003:0> a.object_id
=> 69922343540500


irb(main):004:0> h[a] = 12          #Hash with the array as a key
=> 12
irb(main):005:0> a << 4             #Modified the array
=> [1, 2, 3, 4]
irb(main):006:0> a.object_id        #Object id obviously remains the same.
=> 69922343540500
irb(main):007:0> h[a]               #Hash with the same object_id now returns nil.
=> nil
irb(main):008:0> h                  #Modified hash
=> {[1, 2, 3, 4]=>12}
irb(main):009:0> h[[1,2,3,4]]       #Tried to access the value with the modified key -
=> nil


irb(main):011:0> h.each { |key,value| puts "#{key.inspect} maps #{value}" }
[1, 2, 3, 4] maps 12
=> {[1, 2, 3, 4]=>12}

Now when i iterate over the hash, its possible to identify the map between the key and the value.

Could some one please explain me this behaviour of ruby hash and what are the properties of hash keys.

1) As i mentioned above, the object_id hasn’t changed – then why is the value set to nil.

2) Is there any possible way so that i can get back the value ’12’ from the hash ‘h’ because h[[1,2,3,4]] as mentioned above returns nil.

  • 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-23T16:46:59+00:00Added an answer on May 23, 2026 at 4:46 pm

    Hash keys are checked using the #eql? method, and since [1, 2, 3] isn’t .eql? to [1, 2, 3,4] your hash lookup has a different result.

    Maybe you want to be using something other than an Array as your Hash key if the semantics aren’t working for you?

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

Sidebar

Related Questions

I just want a simple tool that will help me quickly write scripts/packages that
this query will just one row as a result myDataContext db = new myDataContext();
I know it will sound dumb or people will just say: read the documentations
I'm trying to write a simple program in VC++ which will just initialize the
I like to be sure that everything will work just by copying the contents
Say you construct an activerecord query that will always just return a single value,
Is there a cucumber command that will print out just the feature info and
I am working on an application at the minute that will originally be just
For some strange, bizarre reason, my images in my website just will not display
I just wonder whether there will be any conflict if two service intents are

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.