'How to write the filter condition in ruby
I have some code logic which is written in javascript. I am new to ruby and I would like to write the same logic in Ruby. Here is the logic
var accountsToBlock = allExceedersArray.filter(accountId => !blockedExceedersArray.includes(accountId));
Solution 1:[1]
Try something like this
accounts_to_block = all_exceeders_array.select do |account_id|
!blocked_exceeders_array.include?(account_id)
end
Solution 2:[2]
ruby also has a filter method, but I would look at the ruby reject method, as it's more readable to say
all_exceeders.reject { |account_id| blocked_exceeders.include?(account_id) }
than
all_exceeders.filter { |account_id| !blocked_exceeders.include?(account_id) }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ursus |
| Solution 2 | Ben Garcia |
