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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:58:33+00:00 2026-06-17T04:58:33+00:00

How do reject! and reject differ from delete_if for a Hash in Ruby? Can

  • 0

How do reject! and reject differ from delete_if for a Hash in Ruby? Can anyone explain the differences between them with simple code snippets?

  • 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-17T04:58:35+00:00Added an answer on June 17, 2026 at 4:58 am

    Since the other answers are referring to Array#delete_if and not Hash#delete_if, which seems to be what you are asking, I thought I should clarify.

    As others have pointed out, reject and reject! differ in that reject! version modifies the hash in-place, while reject creates a new hash. Meanwhile delete_if is almost the same as reject!.

    In fact, for an Array, reject! and delete_if are exactly the same.

    However, for a Hash, they are slightly different. reject! returns nil if no changes were made, or the hash if changes were made. delete_if always returns the hash.

    hash = {a: 1, b: 2, c: 3}
    
    return_value = hash.delete_if {|k, v| v > 100}
    # hash is unchanged, return_value is {a: 1, b: 2, c: 3}
    
    return_value = hash.reject! {|k, v| v > 100}
    # hash is unchanged, return_value is nil
    

    So if you wanted to check whether changes were made to the hash after deleting the elements, you could use reject! and check the return value.

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

Sidebar

Related Questions

What's the closest equivalent to ruby's Hash#reject/delete_if method? {a:1, b:2, c:3, d:4}.reject { |k,v|
I am a little confused with Enumerator#reject in ruby. Consider the following code: (1..10).select
A little intimidated by reject!, delete_if, select, keep_if, etc. http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-reject What is the shortest
We have a Website where a user can reject something from another user with
Can you explain me what function of merge method here? it should reject email
Hi I have the following code with jQuery: function reject(id) { $.ajax({ type: 'POST',
Does anyone know how to reject an email received through mailgun (mailgun.com)? As a
Anyone know a way to reject input onto a UISegmentedControl object? During my application
Iptables can be configured to reject a packet with an action such as sending
I'm trying to programatically approve/reject workflows in SharePoint. I can successfully do this, but

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.