I wrote the following code, which keeps x within the range (a..b). In pseudo code:
x
(a..b)
(if x < a, x = a; if x > b, x = b)
Ruby 2.4.0 introduces Comparable#clamp:
523.clamp(0, 100) #=> 100