Issue #10594 has been updated by Ferdinand Niedermann.


Benoit Daloze wrote:
> Good luck explaining the precise semantics of clamp on String though.

IMO it's not that hard to grasp:

     call-seq:
        obj.clamp(min, max) ->  obj

    Returns min if obj <=> min is less than zero, max if obj <=> max is
    greater than zero and obj otherwise.

        12.clamp(0, 100)         #=> 12
        523.clamp(0, 100)        #=> 100
        -3.123.clamp(0, 100)     #=> 0

        'd'.clamp('a', 'f')      #=> 'd'
        'z'.clamp('a', 'f')      #=> 'f'


----------------------------------------
Feature #10594: Comparable#clamp
https://bugs.ruby-lang.org/issues/10594#change-54194

* Author: Chris Johnson
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This is basically a re-opening of the feature request of issue#4573 (https://bugs.ruby-lang.org/issues/4574), which was closed due a naming debate.

It seems the standard naming for restricting a number to a specified range is indeed 'clamp'.  (1)(2)(3)

As such, can we use Yusuke Endoh's original patch with the naming adjustments?  If so, I can provide accordingly.

Cheers.

(1) http://www.rubydoc.info/github/epitron/epitools/Numeric:clamp
(2) http://stackoverflow.com/questions/12020787/is-there-a-limit-clamp-function-in-ruby
(3) https://developer.gnome.org/glib/stable/glib-Standard-Macros.html#CLAMP:CAPS


---Files--------------------------------
num_clamp.c (427 Bytes)
comparable-clamp.diff (2.52 KB)


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