If you are using Ruby on Rails or the Facets gem, you can use slice:
original_hash.slice(*keys)
If not, I'd recommend select over reject, for a small readability improvement:
select
reject
new_hash = original_hash.select { |key, _| keys.include?(key.to_s) }
If you are using Ruby on Rails or the Facets gem, you can use slice:
If not, I'd recommend
select
overreject
, for a small readability improvement: