1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Clamp To An Endless Range as a Ruby TIL

This commit is contained in:
jbranchaud
2024-12-25 22:07:58 -07:00
parent 4e5ba0ce4c
commit 855251e478
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
# Clamp To An Endless Range
The
[`Comparable#clamp`](https://ruby-doc.org/3.3.6/Comparable.html#method-i-clamp)
method allows us to specify the bounds of a value we want. If the target value
is between the bounds, then we get that value. Otherwise, we gets the nearest
end of the bounds.
We can even pass a range to `#clamp` instead of separate lower and upper bound
values. Because Ruby has beginless and endless ranges, this gives us the
ergonomics to, say, clamp to any non-negative value with a `0..` endless range.
Here is what that looks like:
```ruby
> 22.clamp(0..)
=> 22
> (-33).clamp(0..)
=> 0
> 0.clamp(0..)
=> 0
```