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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:14:15+00:00 2026-05-25T14:14:15+00:00

I have a hash that looks something like this: params = { :irrelevant =>

  • 0

I have a hash that looks something like this:

params = { :irrelevant => "A String",
           :choice1 => "Oh look, another one",
           :choice2 => "Even more strings",
           :choice3 => "But wait",
           :irrelevant2 => "The last string" }

And I want a simple way to reject all the keys that aren’t choice+int. It could be choice1, or choice1 through choice10. It varies.

How do I single out the keys with just the word choice and a digit or digits after them?

Bonus:

Turn the hash into a string with tab (\t) as a delimiter. I did this, but it took several lines of code. Usually master Rubicians can do it in one or so lines.

  • 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-25T14:14:15+00:00Added an answer on May 25, 2026 at 2:14 pm

    Edit to original answer: Even though this is answer (as of the time of this comment) is the selected answer, the original version of this answer is outdated.

    I’m adding an update here to help others avoid getting sidetracked by this answer like I did.

    As the other answer mentions, Ruby >= 2.5 added the Hash#slice method which was previously only available in Rails.

    Example:

    > { one: 1, two: 2, three: 3 }.slice(:one, :two)
    => {:one=>1, :two=>2}
    

    End of edit. What follows is the original answer which I guess will be useful if you’re on Ruby < 2.5 without Rails, although I imagine that case is pretty uncommon at this point.


    If you’re using Ruby, you can use the select method. You’ll need to convert the key from a Symbol to a String to do the regexp match. This will give you a new Hash with just the choices in it.

    choices = params.select { |key, value| key.to_s.match(/^choice\d+/) }
    

    or you can use delete_if and modify the existing Hash e.g.

    params.delete_if { |key, value| !key.to_s.match(/choice\d+/) }
    

    or if it is just the keys and not the values you want then you can do:

    params.keys.select { |key| key.to_s.match(/^choice\d+/) }
    

    and this will give the just an Array of the keys e.g. [:choice1, :choice2, :choice3]

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

Sidebar

Related Questions

I have some code in my application that looks something like this: char *hash
I am working with a hash that looks something like this: h = {
I have an PHP array parsed from a JSON string that looks like this:
I have YAML data that looks sort of like this, but ~150k of it:
I have a twenty byte hex hash that I would like to store in
I have this pseudo-code in java: bytes[] hash = MD5.hash(example); String hexString = toHexString(hash);
I have a text in an email on a windows box that looks something
I have a class that takes a string in this format: 000067000000000012620060324b38e2cab3353 , encrypts
My code looks like this: hash = MyModel.count(:group => 'id', :conditions => 'bla =
I am currently learning Perl. I have Perl hash that contains references to hashes

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.