San Backups
San Backups

Reputation: 503

Limit value to a maximum

@a = 200
@b = 1

@c = @a / @b

@c is going to equal 200. How can I institute a max value for @c to be 100?

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

In SQL, this is the LEAST function.

Upvotes: 2

Views: 943

Answers (2)

Pritesh Jain
Pritesh Jain

Reputation: 9146

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

Upvotes: -1

Ry-
Ry-

Reputation: 224905

Enumerable#min works:

[@c, 100].min

Upvotes: 7

Related Questions