Issue #10594 has been updated by D.E. Akers.


> I think there is even another option: Swapping the min and max values if they are passed in the wrong order.

I suspect there is little to no precedent for allowing "position-independent positional arguments". It's true that they could be tolerated and correctly handled in the case of `#clamp`, but doing so would likely hide some logic error elsewhere in the program.

`#between?` is a query method and thus observes the widespread convention of returning *only* either `true` or `false`. That nothing exists between 2 on the left and 1 on the right means `#between?` is correct not to raise given such input.

I confess that I only suggested returning the receiver to pad the list. It *would* potentially indicate to the caller that the requested operation couldn't be performed, but that's precisely what exceptions are for.

Silently swapping is antithetical to the very notion of positional arguments, returning `nil` is just going to make something else fail, and returninghe receiver goes against the method's *raison d'ĂȘtre*.

The only potential alternative I see to raising an `ArgumentError` in the case of `min` > `max` would be to make them keyword arguments, but that would break symmetry with `#between?` and feels heavy-handed besides.

----------------------------------------
Bug #10594: Numeric#clamp
https://bugs.ruby-lang.org/issues/10594#change-53320

* Author: Chris Johnson
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 2.1.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
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)


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