source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compar-ext/mrblib/compar.rb@ 446

Last change on this file since 446 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 750 bytes
Line 
1module Comparable
2 ##
3 # Returns <i>min</i> if <i>obj</i> <code><=></code> <i>min</i> is less
4 # than zero, <i>max</i> if <i>obj</i> <code><=></code> <i>max</i> is
5 # greater than zero and <i>obj</i> otherwise.
6 #
7 # 12.clamp(0, 100) #=> 12
8 # 523.clamp(0, 100) #=> 100
9 # -3.123.clamp(0, 100) #=> 0
10 #
11 # 'd'.clamp('a', 'f') #=> 'd'
12 # 'z'.clamp('a', 'f') #=> 'f'
13 #
14 def clamp(min, max)
15 if (min <=> max) > 0
16 raise ArgumentError, "min argument must be smaller than max argument"
17 end
18 c = self <=> min
19 if c == 0
20 return self
21 elsif c < 0
22 return min
23 end
24 c = self <=> max
25 if c > 0
26 return max
27 else
28 return self
29 end
30 end
31end
Note: See TracBrowser for help on using the repository browser.