Skip to content

Instantly share code, notes, and snippets.

@inopinatus
Last active May 10, 2020
Embed
What would you like to do?
ar inequalities
Inequality Interval Ruby range Active Record* Arel**
n >= x [x..∞) x.. where(n: x..) n.gteq(x)
n <= x (-∞..x] ..x where(n: ..x) n.lteq(x)
n < x (-∞..x) ...x where(n: ...x) n.lt(x)
n > x (x..∞) n/a where.not(n: ..x) n.gt(x)
x <= n <= y [x..y] x..y where(n: x..y) n.between(x..y)
x <= n < y [x..y) x...y where(n: x...y) n.between(x...y)
x < n <= y (x..y] n/a where.not(n: ..x).where(n: ..y) n.gt(x).and(n.lteq(y))
x < n < y (x..y) n/a where.not(n: ..x).where(n: ...y) n.gt(x).and(n.lt(y))

* When using predicate builder for Ranges, for reliable scope composition e.g. in the presence of association joins, self-joins, merges etc.
** When n = Model.arel_attribute(:n)

@inopinatus
Copy link
Author

inopinatus commented May 10, 2020

The wheels come off with left-open intervals, for which Ruby has no range literal syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment