Ever wanted an integer to be allowed a specific range of valid numbers rounding when necessary? Well, unfortunately in the Ruby community this has been a point of contention because no built-in function exists. We can however easily replicate the intended behavior through the clever usage of the enumerable sort method.
This may be a bit overkill...
Image property of amazon.com
Because we want to return an integer that has been "clamped" to a specific range we need a way to set a minimum and maximum value. The Enumerable sort method comes in handy for this purpose. We'll be using the sort method while grabbing the second element in the returned array to gather the appropriate clamp value.
All about sort
Enumerable#sort takes an array and attempts to sort the elements either alphabetically or by number unless a custom sort is passed to the block. This allows something like the following to occur.
"Returns an array containing the items in enum sorted, either according to their own <=> method, or by using the results of the supplied block."
For the sake of this post we will use the following integer range of 0 to 10 to set the min and max values. These values will use the sort method to order them in rising order. I will show a couple examples below to demonstrate how this method works.
Pretty slick looking, albeit a bit cryptic. Never fear it is actually quite simple to explain.
By using three integers, one of which is our input integer, and then calling sort on them we receive the integers in rising order from lowest to smallest. The trick here is
.sort. This says sort the previous array and return the 2nd element from it. Since arrays start at 0 then
 means the second or middle element.
[x, a, b].sort
x - input
a - minimum value
b - maximum value
Image property of http://genius.com/
So now we will always return the proper clamped value. Lets take a look at an example of what is happening. I'll reuse the first example from above with comments to disassemble what is happening in the intermediate steps.
There you have it. A simple and easy way to clamp integers between a range. Also you might be wondering why I keep using the phrase clamp. That is because in other programming languages the clamp method is built-in to accomplish this from the start.
Here it is in method form for easy copy and pasting.