3
@a = 200
@b = 1

@c = @a / @b

@c将等于 200。如何设置最大值为@c100?

if @c = 99, do nothing
if @c = 100, do nothing
if @c > 100, make @c 100

在 SQL 中,这是LEAST函数。

4

2 回答 2

7

Enumerable#min作品:

[@c, 100].min
于 2012-08-26T15:49:26.300 回答
-1
case @c
 when  99
   ##   do something
 when  100
   ##   do something
 else
  @c = 100 if @c > 100 
  # or
  @c = [@c, 100].min # inspired by minitech answer
end
于 2012-08-26T16:02:17.123 回答