Feature #14784

Comparable#clamp with a range

Added by zverok (Victor Shepelev) over 1 year ago. Updated about 2 months ago.

Target version:



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, 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. I believe this is pretty natural:

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


  1. This is "one-sided", you can't do clamp( 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
  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:
# with clamp

# 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.


Updated by shevegen (Robert A. Heiler) over 1 year ago

Considering that Ranges allow a ruby hacker to omit the end value,
for infinity/endless, since about ... a month or so, I think your
example makes sense in this regard, e. g.

begin .. end

being the same as:

begin ..


Perhaps also the converse, but I have to admit that
all these examples look very strange to my eyes. Like:


I always look at it as if something is missing. Personally
I prefer explicit "begin .. end".


clamp(min: 0, max: 10)

seems to be a nice API, in my opinion. At the least the names "min"
and "max" appear explicit and make sense (to me).

I agree, mostly for consistency, that if endless range has been
accepted, being able to do so via #clamp may seem a logical
continuation (to me). I am mostly neutral to the issue though,
as I do not (yet) use clamp in my own ruby code.

Updated by nobu (Nobuyoshi Nakada) over 1 year ago

zverok (Victor Shepelev) wrote:

  1. Why do you need one-sided clamp at all? Because alternatives is much more wordy, making reader think:

Why not [chain.of.calculations, 0].max?

Updated by zverok (Victor Shepelev) over 1 year ago

nobu (Nobuyoshi Nakada)

Why not [chain.of.calculations, 0].max?

Because this chain.of.calculations in reality could be something like

  .map { |speaker, paragraphs| paragraph.sort_by(&:length).first }
  .sort_by { |para| para.words.count }, 3].max

Jumping to start and end of this super-huge array while writing and reading is a pain.

So, now I'll probably write something like

  .map { |speaker, paragraphs| paragraph.sort_by(&:length).first }
  .sort_by { |para| para.words.count }
  .yield_self { |len| [len, 3].max }

...which is OK-ish, but I never really liked how [value, MIN].max represents the idiom "limit this number to minimum possible value". The [value, MIN] somehow represents two values as equally important, while in fact one of them is "the main calculated value", and another one is "just one parameter of the calculation".

So, for me, this looks 200% better:

  .map { |speaker, paragraphs| paragraph.sort_by(&:length).first }
  .sort_by { |para| para.words.count }

Updated by akr (Akira Tanaka) about 1 year ago

I feel this proposal is needlessly big: it needs range support for Comparable#clamp and startless range.
I think just supporting nil for 1st and 2nd argument of Comparable#clamp is enough.

Updated by zverok (Victor Shepelev) about 1 year ago

akr (Akira Tanaka) The proposal is "Comparable#clamp with a range". It also justifies the possible need for a startless range, which is extracted to #14799.

I believe that clamp(3..), clamp(..10) is 200% more Ruby-idiomatic than clamp(3, nil), clamp(nil, 10).
But even WITHOUT startless range, I believe that "clamp accepting Range" is just natural.

Updated by matz (Yukihiro Matsumoto) about 1 year ago

Please don't put multiple proposals in one issue. It's hard for us to tell which is important clamp to accept ranges or having one-sided clamp. This kind of mixture leads to our confusion that hinders final decision.


Updated by shyouhei (Shyouhei Urabe) about 1 year ago

  • Subject changed from One-sided Comparable#clamp to Comparable#clamp with a range

So, let's focus on making Comparable#clamp accept Ranges. Subject updated.

Updated by akr (Akira Tanaka) about 1 year ago

If Ruby support clamp(range), the behavior of clamp(b...e) should be considered.

What returns x.clamp(b...e) when e < x ?

10.clamp(0...20) would return 19.

But There is Rational.
How about 10r.clamp(0r...20r) ?
"Maximum Rational value less than 20" is not exist.

Updated by jonathanhefner (Jonathan Hefner) 6 months ago

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.

Updated by svoop (Sven Schwyn) about 2 months ago

Here's a real life use case for clamp with Range support:

Our Rails app has quite a few app settings which define ranges of permitted values e.g. goal_range = (10..1000). Values outside of those ranges are clamped which leads to things like:

value.clamp(Rails.application.config.x.goal_range.min, Rails.application.config.x.goal_range.max)

It is IMO more readable if clamp accepts Ranges:


Updated by matz (Yukihiro Matsumoto) about 2 months ago

Accepted. It should raise an error on end-exclusive ranges (...).


Also available in: Atom PDF