0011 Clamp

A bounded method!

The clamp method is defined in the Comparable module. It is used to bound a value. It is more efficient and explicit than the « classic » [[1, 3].max, 4].min.

0011-clamp_1.png

The min and max arguments can be nil to indicate no limit.

0011-clamp_2.png

A Range argument can be used.

0011-clamp_3.png

⚠️ The Range must be inclusive. The type of object returned depends on arguments.

0011-clamp_4.png

Link to Ruby documentation for clamp.