Created
June 4, 2020 16:57
-
-
Save ahmad19/3d9c0232f71217b6020d50dc29e634c3 to your computer and use it in GitHub Desktop.
Search through array of objects by hash in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ItemCollection | |
def where(data) | |
items.select do |item| | |
item.fetch(data.keys.first) == data.values.first.to_s | |
end.map { |item| Item.new(item) } | |
end | |
end | |
item_collection = ItemCollection.new | |
item_collection.where({merchant_id: 2}) | |
=> [<Item>, <Item>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment