exclude
rel.exclude(Predicate.eq(:a, "foo"))rel.exclude(a: "foo", b: "bar", ...)rel.exclude(->(t) { t[:a] == "foo" })rel.exclude({ a: "foo" })rel.exclude({ a: "foo", b: "bar" })rel.exclude(t => t.a === "foo")Problem
Section titled “Problem”Remove tuples from a relation that match a given criterion.
Example: Which products are NOT discontinued?
Description
Section titled “Description”The exclude operator is the inverse of restrict. While restrict keeps tuples that match the predicate, exclude removes them.
exclude(predicate) is equivalent to restrict(NOT predicate).
Predicates can be specified as Predicate objects (e.g., Predicate.eq(:status, "inactive")), a hash for equality matching (e.g., { status: "inactive" }), or a lambda that returns true for tuples to exclude.
Predicates can be specified as an object with attribute-value pairs for equality matching ({ status: "inactive" }) or a function that receives a tuple and returns a boolean (t => t.status === "inactive").
Requirements
Section titled “Requirements”The specified attributes referenced by the predicate must be part of the input relation’s heading.
Examples
Section titled “Examples”Consult the Overview page for the data model used in these examples.
Basic exclude
Section titled “Basic exclude”# Get all suppliers except those in Parissuppliers.exclude(city: "Paris").to_a
=>[{:sid=>"S1", :name=>"Smith", :status=>20, :city=>"London"}, {:sid=>"S4", :name=>"Clark", :status=>20, :city=>"London"}, {:sid=>"S5", :name=>"Adams", :status=>30, :city=>"Athens"}]// Get all suppliers except those in Parissuppliers.exclude({ city: "Paris" }).toArray()
// =>// [{ sid: "S1", name: "Smith", status: 20, city: "London" },// { sid: "S4", name: "Clark", status: 20, city: "London" },// { sid: "S5", name: "Adams", status: 30, city: "Athens" }]With function predicate
Section titled “With function predicate”# Exclude suppliers with status >= 30suppliers.exclude(->(t) { t[:status] >= 30 }).to_a
=>[{:sid=>"S1", :name=>"Smith", :status=>20, :city=>"London"}, {:sid=>"S2", :name=>"Jones", :status=>10, :city=>"Paris"}, {:sid=>"S4", :name=>"Clark", :status=>20, :city=>"London"}]// Exclude suppliers with status >= 30suppliers.exclude(t => t.status >= 30).toArray()
// =>// [{ sid: "S1", name: "Smith", status: 20, city: "London" },// { sid: "S2", name: "Jones", status: 10, city: "Paris" },// { sid: "S4", name: "Clark", status: 20, city: "London" }]Comparison with restrict
Section titled “Comparison with restrict”exclude and restrict are complementary operations:
# These produce the same result:suppliers.exclude(city: "Paris")suppliers.restrict(Predicate.neq(:city, "Paris"))
# Together they partition the relation:paris = suppliers.restrict(city: "Paris")not_paris = suppliers.exclude(city: "Paris")# paris.union(not_paris) equals suppliers// These produce the same result:suppliers.exclude({ city: "Paris" })suppliers.restrict(t => t.city !== "Paris")
// Together they partition the relation:const paris = suppliers.restrict({ city: "Paris" })const notParis = suppliers.exclude({ city: "Paris" })// paris.union(notParis) equals suppliers