How to remove a key from Hash and get the remaining hash in Ruby/Rails?
In Ruby, removing a key from a hash can be done in several ways, depending on whether you want to mutate (modify) the original hash or return a new hash with the specified key removed. If you’re working in a Rails environment, you have an additional helper method at your disposal. Below, you’ll see both the pure Ruby and Rails approaches.
1. Pure Ruby Approaches
1.1 Using delete
(Mutates the Original Hash)
The delete
method removes a key-value pair from the original hash and returns the value of the deleted entry:
my_hash = { a: 1, b: 2, c: 3 } deleted_value = my_hash.delete(:b) puts deleted_value # => 2 puts my_hash # => {:a=>1, :c=>3}
- Pros: Simple, directly supported by Ruby.
- Cons: Mutates the original hash, which may not be desired in some scenarios.
1.2 Using reject
or select
(Returns a New Hash)
If you want to keep the original hash intact and return a new one without a certain key, you can use Hash#reject
:
my_hash = { a: 1, b: 2, c: 3 } remaining_hash = my_hash.reject { |k, _v| k == :b } puts remaining_hash # => {:a=>1, :c=>3} puts my_hash # => {:a=>1, :b=>2, :c=>3}
- Pros: Non-destructive; returns a new hash and leaves
my_hash
unchanged. - Cons: Slightly more verbose. Also, you need an explicit condition inside the block.
Alternatively, you could duplicate the hash and then delete
:
my_hash = { a: 1, b: 2, c: 3 } remaining_hash = my_hash.dup remaining_hash.delete(:b) puts remaining_hash # => {:a=>1, :c=>3} puts my_hash # => {:a=>1, :b=>2, :c=>3}
2. Rails Approach: except
Rails (specifically Active Support) provides a handy method called except
that returns a new hash without the specified keys. This is often the cleanest approach if you’re in a Rails application.
my_hash = { a: 1, b: 2, c: 3 } remaining_hash = my_hash.except(:b) puts remaining_hash # => {:a=>1, :c=>3} puts my_hash # => {:a=>1, :b=>2, :c=>3}
Why Use except
?
- Non-Destructive: It returns a new hash, leaving the original intact.
- Readability: Expresses intent clearly (“all keys except these”).
- Multiple Keys: You can remove multiple keys in one call, e.g.
my_hash.except(:b, :c)
.
Best Practice Recommendations
-
Mutating vs. Non-Mutating
Decide if you truly want to modify your original hash. For many use cases—especially in functional or side-effect-free contexts—non-mutating methods (reject
,except
) are preferable. -
Rails Convenience
If you’re already in a Rails environment,except
is often the most straightforward approach. It’s more expressive than manually writing areject
block. -
Performance Considerations
For large hashes, bothreject
andexcept
create new hash objects. While this is typically negligible for most use cases, if performance is critical and you don’t need the original, mutating viadelete
may be slightly more efficient.
Boost Your Ruby and Rails Skills
Handling hashes is just one piece of the broader skill set you need as a Ruby/Rails developer. To deepen your expertise in coding patterns and system design, consider these DesignGurus.io courses:
-
Grokking the Coding Interview: Patterns for Coding Questions
Master the most common coding patterns tested in top-tier interviews. -
Grokking System Design Fundamentals
Learn the essentials of building scalable, fault-tolerant systems—perfect for growing Rails applications.
For tailored feedback, you can also book a Coding Mock Interview or System Design Mock Interview with ex-FAANG engineers.
Conclusion
In Ruby, you can remove keys from a hash using methods like delete
(mutates the original hash) or reject
(creates a new hash). In Rails, except
is a handy, non-destructive method that returns a new hash minus the specified keys. By choosing the right approach for your use case—mutating vs. non-mutating—you can write clearer, more maintainable code.