Skip to content

exclude

rel.exclude(Predicate.eq(:a, "foo"))
rel.exclude(a: "foo", b: "bar", ...)
rel.exclude(->(t) { t[:a] == "foo" })

Remove tuples from a relation that match a given criterion.

Example: Which products are NOT discontinued?

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.

The specified attributes referenced by the predicate must be part of the input relation’s heading.

Consult the Overview page for the data model used in these examples.

# Get all suppliers except those in Paris
suppliers.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"}]
# Exclude suppliers with status >= 30
suppliers.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 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