Skip to content

Instantly share code, notes, and snippets.

Last active May 10, 2020
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 > x (x..∞) n/a where.not(n: ..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)
x < n < y (x..y) n/a where.not(n: ..x).where(n: ...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)

Copy link

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