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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:45:57+00:00 2026-06-06T01:45:57+00:00

To add a new pair to Hash I do: {:a => 1, :b =>

  • 0

To add a new pair to Hash I do:

{:a => 1, :b => 2}.merge!({:c => 3})   #=> {:a => 1, :b => 2, :c => 3}

Is there a similar way to delete a key from Hash ?

This works:

{:a => 1, :b => 2}.reject! { |k| k == :a }   #=> {:b => 2}

but I would expect to have something like:

{:a => 1, :b => 2}.delete!(:a)   #=> {:b => 2}

It is important that the returning value will be the remaining hash, so I could do things like:

foo(my_hash.reject! { |k| k == my_key })

in one line.

  • 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-06T01:46:00+00:00Added an answer on June 6, 2026 at 1:46 am

    For those of you who just came here to know how to delete a key/value pair from a hash, you can use:
    hash.delete(key)

    For the rest of you who came here to read a wall of text about something entirely different, you can read the rest of this answer:

    Rails has an except/except! method that returns the hash with those keys removed. If you’re already using Rails, there’s no sense in creating your own version of this.

    class Hash
      # Returns a hash that includes everything but the given keys.
      #   hash = { a: true, b: false, c: nil}
      #   hash.except(:c) # => { a: true, b: false}
      #   hash # => { a: true, b: false, c: nil}
      #
      # This is useful for limiting a set of parameters to everything but a few known toggles:
      #   @person.update(params[:person].except(:admin))
      def except(*keys)
        dup.except!(*keys)
      end
    
      # Replaces the hash without the given keys.
      #   hash = { a: true, b: false, c: nil}
      #   hash.except!(:c) # => { a: true, b: false}
      #   hash # => { a: true, b: false }
      def except!(*keys)
        keys.each { |key| delete(key) }
        self
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to AWS and created key pair correctly @air~/.ec2 - 14:25:10$ ec2-add-keypair
How can I add a new key/value pair to an existing array inside of
how to add new column in new DataTable But Erorr private void LoadtoList() {
How add key-value pair to instance on HashMultiMap that is member of declared by
I want to add a new NewKey-NewValue pair to my config properties file in
Dataset extends ArrayList. Dataset<Pair<SRGB>> data = new Dataset<Pair<SRGB>>(); Statement stmt = this.conn.createStatement(); ResultSet rs
I want to add key pair values in plist . I dont know how
My code is like this: Dictionary<string, string> specialCharacters = new Dictionary<string, string>(); specialCharacters.Add(@, %);
How to add new hyperlink column to an asp.net gridview where columns are autogenerated?
How to add new css class or id and then write properties for html

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.