Issue #14784 has been updated by jonathanhefner (Jonathan Hefner).


I agree this would be a useful feature.  I have a library that implements this as `Comparable#at_least(min)` and `Comparable#at_most(max)`, but `clamp(min..)` and `clamp(..max)` feel more idiomatic (although they do require an extra object allocation).

> 10.clamp(0...20) would return 19.
> But There is Rational.
> How about 10r.clamp(0r...20r) ?

This is also a concern.  I think the most appropriate behavior, unfortunately, is to raise an ArgumentError if `range.end && range.exclude_end?`.  Maybe `Integer#clamp` could override this behavior only when the range end is also an Integer, but the convenience may not justify the extra complexity and possible inconsistency.


----------------------------------------
Feature #14784: Comparable#clamp with a range
https://bugs.ruby-lang.org/issues/14784#change-77861

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
**Proposal**

Allow "one-sided" `clamp` to limit only upper bound (and, ideally, only lower too).

Proposed implementation: allow `clamp(begin..end)` call sequence (without deprecating `clamp(begin, end)`), to take advantage from open-ended ranges with `clamp(begin..)`.

**Reasoning about range**

I looked through `#clamp` [discussion](https://bugs.ruby-lang.org/issues/10594), but couldn't find there why syntax `clamp(b, e)` was preferred to `clamp(b..e)`. The only one I could think of is possible confuse of how `clamp(b..e)` and `clamp(b...e)` behaviors should differ.

The problem becomes more important with the introduction of [open-ended ranges](https://bugs.ruby-lang.org/issues/12912). I believe this is pretty natural:

```ruby
some_calculation.clamp(0..)    # now, I use clamp(0, Float::INFINITY)
timestamp.clamp(Date.today..)  # now, I typically use clamp(Date.today..INFINITE_FUTURE_DATE) with custom defined constant
```

Counter-arguments:

1. This is "one-sided", you can't do `clamp(..Date.today)`. To this I can answer than from my experience "clamping only minimum" is more frequent, and if you need to clamp only maximum, most of the time there is some "reasonable minumum". Another idea is that maybe this is a proof why "start-less" ranges are necessary, after all, [doubted here](https://bugs.ruby-lang.org/issues/12912#note-12)
2. Why not just leave current `clamp(b, e)` and allow `clamp(b)`? Answer: because when you see `clamp(10)`, is it `clamp(10, nil)`, or `clamp(nil, 10)` (yes, logically it is the first argument that is left, but from readability point of view it is not that obvious). Possible alternative: `clamp(min: 0, max: 10)`, where you can omit any of two.
3. Why do you need one-sided clamp at all? Because alternatives is much more wordy, making reader think:

```ruby
# with clamp
chain.of.calculations.clamp(0..)

# without clamp
v = chain.of.calculations
v < 0 ? 0 : v

# or, with yield_self (renamed to then)
chain.of.calculations.then { |v| v < 0 ? 0 : v }
```

Both alternatives "without `#clamp`" shows intentions much less clear.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>