Skip to content

Instantly share code, notes, and snippets.

@ahmad19
Created June 4, 2020 16:57
Show Gist options
  • Save ahmad19/3d9c0232f71217b6020d50dc29e634c3 to your computer and use it in GitHub Desktop.
Save ahmad19/3d9c0232f71217b6020d50dc29e634c3 to your computer and use it in GitHub Desktop.
Search through array of objects by hash in ruby
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