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

  • Home
  • SEARCH
  • 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 7440407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:50:48+00:00 2026-05-29T10:50:48+00:00

Why does Ruby hash an integer n to 2 * n + 1 ?

  • 0

Why does Ruby hash an integer n to 2 * n + 1?

>> [0,1,2,3].each {|x| puts x.hash}
1
3
5
7

I can see that you don’t always need to have complicated hashes, especially for simple objects. But why the ‘double and add 1’ rule as opposed to doing what Python does, which is to hash integers to themselves?

>>> map(hash,[0,1,2,3])
[0, 1, 2, 3]

Is there a reason?

  • 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-29T10:50:49+00:00Added an answer on May 29, 2026 at 10:50 am

    Integers are objects, so they have an object_id. But there is an infinite number of integers. Seemingly, no room for other objects. How does Ruby pull this off?

    10.times{|i| puts i.object_id}
    

    Output:

    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    

    Integers take all odd object_id’s, the rest of the objects go in between, they use the even numbers. The conversion from object_id (and hash) to integer (and vice versa) is very easy: chop the rightmost 1 bit (or add it).

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

Sidebar

Related Questions

Does Ruby have any Formatter classes or methods that can be used to format
In Ruby, I have a hash of objects. Each object has a type and
Does Ruby have a some_string.starts_with(abc) method that's built in?
Does Ruby have any tools along the lines of pylint for analyzing source code
Does Ruby (or Rails) provide an easy way to accomplish the following: if @author.articles.each(:published
In order to implement auto-vivification of Ruby hash, one can employ the following class
I have an issue dealing with a hash of objects. My hashes are player
I have a function in Ruby: def find_item(keyword) potential = [] $items.each do |item|
Does Ruby have a plain-English keyword for exclusive or, like they have and and
I have an array, and I want to make a hash so I can

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.