diff --git a/ChangeLog b/ChangeLog index 694edeb..c73d4ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Jul 9 18:31:49 2015 Ferdinand Niedermann + + * compar.c (cmp_clamp): Introduce Comparable#clamp [Bug #10594] + Thu Jul 9 15:07:12 2015 NAKAMURA Usaku * win32/win32.c (waitpid): return immediately if interrupted. diff --git a/compar.c b/compar.c index 946d0e0..e33811e 100644 --- a/compar.c +++ b/compar.c @@ -173,6 +173,35 @@ cmp_between(VALUE x, VALUE min, VALUE max) return Qtrue; } + +/* + * call-seq: + * obj.clamp(min, max) -> obj + * + * Returns min if obj <=> min is less + * than zero, max if obj <=> min 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' + */ + +static VALUE +cmp_clamp(VALUE x, VALUE min, VALUE max) +{ + if (RTEST(cmp_gt(min, max))) { + rb_raise(rb_eArgError, "min argument must be smaller than max argument"); + } + + if (RTEST(cmp_lt(x, min))) return min; + if (RTEST(cmp_gt(x, max))) return max; + return x; +} + /* * The Comparable mixin is used by classes whose objects * may be ordered. The class must define the <=> operator, @@ -225,6 +254,7 @@ Init_Comparable(void) rb_define_method(rb_mComparable, "<", cmp_lt, 1); rb_define_method(rb_mComparable, "<=", cmp_le, 1); rb_define_method(rb_mComparable, "between?", cmp_between, 2); + rb_define_method(rb_mComparable, "clamp", cmp_clamp, 2); cmp = rb_intern("<=>"); } diff --git a/test/ruby/test_comparable.rb b/test/ruby/test_comparable.rb index dab6be5..e40ae16 100644 --- a/test/ruby/test_comparable.rb +++ b/test/ruby/test_comparable.rb @@ -75,6 +75,20 @@ class TestComparable < Test::Unit::TestCase assert_equal(true, @o.between?(0, 0)) end + def test_clamp + cmp->(x) do 0 <=> x end + assert_equal(1, @o.clamp(1, 2)) + assert_equal(-1, @o.clamp(-2, -1)) + assert_equal(0, @o.clamp(-1, 3)) + + assert_equal(1, @o.clamp(1, 1)) + assert_equal(0, @o.clamp(0, 0)) + + assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') { + @o.clamp(2, 1) + } + end + def test_err assert_raise(ArgumentError) { 1.0 < nil } assert_raise(ArgumentError) { 1.0 < Object.new }