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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:51:56+00:00 2026-06-13T17:51:56+00:00

i thought i’m a Ruby giant when i wrote this oneliner: # having this

  • 0

i thought i’m a Ruby giant when i wrote this oneliner:

# having this hash
hash = { 'Portugal' => 1, 'France' => 2, 'USA' => 3 }

# country_id comes from input
country_name = (hash.select { |k,v| v == country_id.to_i }.first || []).first

it does correctly extract the country name and does Not fail if country not found.

i’m totally pleased with it.

however my tutor says it can/should be optimized in terms of readability, length and performance!

what can be clearer/faster than this?

please advise

  • 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-13T17:51:57+00:00Added an answer on June 13, 2026 at 5:51 pm

    well, seems your tutor is right 🙂

    you can do like this:

    hash.invert[ country_id.to_i ] # will work on all versions
    

    or, as suggested by @littlecegian

    hash.key( country_id.to_i )    # will work on 1.9 only
    

    or, as suggested by @steenslag

    hash.index( country_id.to_i )  # will work on 1.8 and 1.9, with a warning on 1.9
    

    Full example:

    hash = { 'Portugal' => 1, 'France' => 2, 'USA' => 3 }
    
    %w[2 3 1 blah].each do |country_id|
    
      # all versions
      country_name = hash.invert[ country_id.to_i ]
    
      # 1.9 only
      country_name = hash.key( country_id.to_i )
    
      # 1.8 and 1.9, with a warning on 1.9
      country_name = hash.index( country_id.to_i )
    
    
      printf "country_id = %s, country_name = %s\n", country_id, country_name
    end
    

    will print:

    country_id = 2, country_name = France
    country_id = 3, country_name = USA
    country_id = 1, country_name = Portugal
    country_id = blah, country_name =
    

    see it running here

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

Sidebar

Related Questions

Thought my range of search options would easily find this. I wish to combine
I thought I had resolved this but I obviously haven't and was hoping someone
I thought this would be fairly simple but it turns out not to work
I thought I'd find more about this topic but I didn't. I have to
I thought I would post this here not so much as a question but
Thought I understood how classes work, then I tried this code: class user {
I thought this would be a pretty common question to look for in Google
I thought this would be pretty academic but its not. I'm trying to traverse
I thought this would be fairly trivial but it's turned out be more difficult
I thought this would be easy, but after scanning many pages Google hasn't come

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.