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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:26:51+00:00 2026-06-17T04:26:51+00:00

I first ran the below Part I of code and get the desired output,

  • 0

I first ran the below Part I of code and get the desired output, now just to play around it and to do some research I did Part II.

Part I

irb(main):001:0> h1 = { "a" => 100, "b" => 200, :c => "c" }
=> {"a"=>100, "b"=>200, :c=>"c"}
irb(main):002:0> h1["a"]
=> 100
irb(main):002:0> h1[:c]
=> "c"

Part II

irb(main):003:0> h1.compare_by_identity
=> {"a"=>100, "b"=>200, :c=>"c"}
irb(main):004:0> h1.compare_by_identity?
=> true
irb(main):005:0> h1["a"]
=> nil
irb(main):006:0> h1[:c]
=> "c"
irb(main):007:0>

How does h1["a"] give different values in Part I and Part II but not the same happened with h1[:c]?

I’m using Ruby 1.9.3.

  • 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-17T04:26:52+00:00Added an answer on June 17, 2026 at 4:26 am

    The docs for compare_by_identity say this:

    Makes hsh compare its keys by their identity, i.e. it will consider exact same objects as same keys.

    Normally, hash keys will be matched using eql?, but compare_by_identity will instead match them using equal? (which is roughly equivalent to comparing object_ids*). Since different instances of a string with the same value have different object_ids, it doesn’t match. However, symbols always have the same object_id, so it does continue to match.

    hash = { 'a' => 'str a', 'b' => 'str b', :c => 'sym c' }
    hash.compare_by_identity
    
    hash.keys.map(&:object_id)      #=> [70179407935200, 70179407935180, 358408]
    ['a', 'b', :c].map(&:object_id) #=> [70179405705480, 70179405705460, 358408]
    
    hash.keys.zip(['a', 'b', :c]).map { |pair| pair.inject(:eql?) }   #=> [true, true, true]
    hash.keys.zip(['a', 'b', :c]).map { |pair| pair.inject(:equal?) } #=> [false, false, true]
    
    a_key = hash.keys.first  #=> 'a'
    hash['a']   #=> nil
    hash[a_key] #=> 'str a'
    hash[:c]    #=> 'sym c'
    

    As you can see, the object_ids for the strings do not match the object_ids of the keys in the hash, but the symbol does. In fact, if you run 'a'.object_id & 'b'.object_id (or call object_id on any string) repeatedly, you will get a different value each time.

    *The caveat here is that one could override object_id, but it will have no effect on the comparison since equal? doesn’t actually use object_id. Further, redefining equal? will change this, but doing so is not recommended explicitly in the Ruby docs.

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

Sidebar

Related Questions

I ran the code below expecting flow to be locked on the 2nd time
After implementing the C++ code below, I ran valgrind --leak-check=full in order to check
I ran across some C++ code recently that typedef ed a struct in a
My application ran fine the first few times and now has decided to stop
I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran
Trying to create the first project. First got the below error when I ran,
I compiled & ran the code pasted below and surprisingly it worked without errors.
Test case below. Output: custom loading of: pkg.TestRun ran. wish this would run in
I got this error when I ran my very first spring example code. It
I first ran into this when trying to determine the relative performance of two

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.