This blog is part of our Ruby 2.4 series.
In Ruby 2.4, clamp method is added to the Comparable module. This method can be used to clamp an object within a specific range of values.
clamp method takes min and max as two
arguments to define the range of values
in which the given argument should be clamped.
clamp can be used to keep
a number within the range of min, max.
Similarly, strings can also be clamped within a range.
Internally, this method relies on applying the spaceship <=> operator between the object and the min & max arguments.