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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:37:51+00:00 2026-05-26T04:37:51+00:00

Is it appropriate to use integers as keys in a Ruby hash? Every example

  • 0

Is it appropriate to use integers as keys in a Ruby hash?

Every example from documentation shows a string or symbol being used as a key, but never an integer.

Internally, would integers somehow get converted to strings? I have seen some conflicting information on the subject.

In other words, is there any significant disadvantage to using integer keys to a hash?

  • 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-26T04:37:51+00:00Added an answer on May 26, 2026 at 4:37 am

    of course you can use integers as keys…

    h = {1 => 'one', 2 => 'two', 3 => 'three'}
    
    (1..3).each do |i|
      puts h[i]
    end
     
    

    =>

    one
    two
    there
    

    irb is your friend! try it..


    In fact you can use any Ruby object as the key (or the value).
    We usually don’t think about using Hashes like this, but it could be quite useful.

    Edit:
    As Óscar López points out, the object just has to respond to .hash for it to work as a key in a Ruby Hash.

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

Sidebar

Related Questions

When is it appropriate to use CoTaskMemAlloc? Can someone give an example?
Let's collect some tips for evaluating the appropriate use of global.asax.
When is it appropriate to use an unsigned variable over a signed one? What
When is it appropriate to use <cflock scope=application> or it's ilk as opposed to
When is it appropriate to use openGl-es on the iPhone versus other toolkits? I
When is it appropriate to use connection pooling and what happens when the maximum
I often find it confusing as to when it is appropriate to use: rs.Close
It's possible, but is it appropriate to use SHFileOperation within a Windows service? All
How are file descriptors and file pointers related? When is it appropriate to use
The short form of this question: When, if ever, is it appropriate to use

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.