title: Is there a method to limit/clamp a number?
body: <p>I wrote the following code, which keeps <code>x</code> within the range <code>(a..b)</code>. In pseudo code: </p>
  
  <pre><code>(if x &lt; a, x = a; if x &gt; b, x = b)
  </code></pre>
  
  <p>In Ruby it would be something like:</p>
  
  <pre><code>x = [a, [x, b].min].max
  </code></pre>
  
  <p>As it is quite basic and useful function, I was wondering if there is a native method to do that in ruby.</p>
  
  <p>As of Ruby 2.3.3 there is apparently no method like this, what would be the shortest/more readable way to do it?</p>
  
  <p>I found:</p>
  
  <pre><code>x = [a, x, b].sort[1]
  </code></pre>
  
  <p>so far, but I'm not sure if it is more readable.</p>