You can use Hash#transform_values to iterate over each value from the hash, modify them when they're arrays and return a new hash:
data = { "within" => ["FID6", "S5"], "uri"=>"/repositories/2/raps/7", "is_repository_default"=>false } data.transform_values { |value| value.is_a?(Array) ? value - ['FID6'] : value } # {"within"=>["S5"], "uri"=>"/repositories/2/raps/7", "is_repository_default"=>false}
Or to map a new hash starting from what you have:
data.map { |key, value| [key, value.is_a?(Array) ? value - ['FID6'] : value] }.to_h # {"within"=>["S5"], "uri"=>"/repositories/2/raps/7", "is_repository_default"=>false} data.to_h { |key, value| [key, value.is_a?(Array) ? value - ['FID6'] : value] } # {"within"=>["S5"], "uri"=>"/repositories/2/raps/7", "is_repository_default"=>false}
Hash#to_h accepts a block since Ruby 2.6