Restrict a number to upper/lower bounds?

Posted by Amy on Stack Overflow See other posts from Stack Overflow or by Amy
Published on 2010-06-16T17:44:00Z Indexed on 2010/06/16 17:52 UTC
Read the original article Hit count: 175

Filed under:
|

Is there a built-in way or a more elegant way of restricting a number num to upper/lower bounds in Ruby or in Rails?

e.g. something like:

def number_bounded (num, lower_bound, upper_bound)
  return lower_bound if num < lower_bound
  return upper_bound if num > upper_bound
  num
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby