[前][次][番号順一覧][スレッド一覧]

ruby-changes:43790

From: nobu <ko1@a...>
Date: Thu, 11 Aug 2016 16:24:29 +0900 (JST)
Subject: [ruby-changes:43790] nobu:r55863 (trunk): Comparable#clamp

nobu	2016-08-11 16:24:25 +0900 (Thu, 11 Aug 2016)

  New Revision: 55863

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55863

  Log:
    Comparable#clamp
    
    * compar.c (cmp_clamp): Introduce Comparable#clamp.  [Feature #10594]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/compar.c
    trunk/test/ruby/test_comparable.rb
Index: compar.c
===================================================================
--- compar.c	(revision 55862)
+++ compar.c	(revision 55863)
@@ -174,6 +174,39 @@ cmp_between(VALUE x, VALUE min, VALUE ma https://github.com/ruby/ruby/blob/trunk/compar.c#L174
 }
 
 /*
+ *  call-seq:
+ *     obj.clamp(min, max) ->  obj
+ *
+ * Returns <i>min</i> if <i>obj</i> <code><=></code> <i>min</i> is less
+ * than zero, <i>max</i> if <i>obj</i> <code><=></code> <i>max</i> is
+ * greater than zero and <i>obj</i> 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)
+{
+    int c;
+
+    if (cmpint(min, max) > 0) {
+	rb_raise(rb_eArgError, "min argument must be smaller than max argument");
+    }
+
+    c = cmpint(x, min);
+    if (c == 0) return x;
+    if (c < 0) return min;
+    c = cmpint(x, max);
+    if (c > 0) return max;
+    return x;
+}
+
+/*
  *  The <code>Comparable</code> mixin is used by classes whose objects
  *  may be ordered. The class must define the <code><=></code> operator,
  *  which compares the receiver against another object, returning -1, 0,
@@ -225,4 +258,5 @@ Init_Comparable(void) https://github.com/ruby/ruby/blob/trunk/compar.c#L258
     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);
 }
Index: test/ruby/test_comparable.rb
===================================================================
--- test/ruby/test_comparable.rb	(revision 55862)
+++ test/ruby/test_comparable.rb	(revision 55863)
@@ -76,6 +76,20 @@ class TestComparable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_comparable.rb#L76
     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 }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55862)
+++ ChangeLog	(revision 55863)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Aug 11 16:24:23 2016  nerdinand  <nerdinand@n...>
+
+	* compar.c (cmp_clamp): Introduce Comparable#clamp.  [Feature #10594]
+
 Thu Aug 11 03:16:59 2016  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* lib/prime.rb: Optimize prime?
Index: NEWS
===================================================================
--- NEWS	(revision 55862)
+++ NEWS	(revision 55863)
@@ -30,6 +30,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L30
     This is different from Enumerable#sum in that Array#sum doesn't depend on
     the definition of each method.
 
+* Comparable
+
+  * Comparable#clamp.  [Feature #10594]
+
 * Dir
 
   * Dir.empty?.  [Feature #10121]

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]