File: compar.rb

package info (click to toggle)
groonga 9.0.0-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,496 kB
  • sloc: ansic: 608,707; ruby: 35,042; xml: 23,643; cpp: 10,319; sh: 7,453; yacc: 5,968; python: 3,033; makefile: 2,609; perl: 133
file content (31 lines) | stat: -rw-r--r-- 750 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Comparable
  ##
  # 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'
  #
  def clamp(min, max)
    if (min <=> max) > 0
      raise ArgumentError, "min argument must be smaller than max argument"
    end
    c = self <=> min
    if c == 0
      return self
    elsif c < 0
      return min
    end
    c = self <=> max
    if c > 0
      return max
    else
      return self
    end
  end
end