views:

64

answers:

3

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
+5  A: 

You can use min and max to make the code more concise:

number_bounded = [lower_bound, [upper_bound, num].min].max
Mark Byers
This is possibly the faster way to do it than the sort() version below. Thanks.
Amy
+2  A: 

Here's a clever way to do it:

[lower_bound, num, upper_bound].sort[1]

But that's not very readable. If you only need to do it once, I would just do

num < lower_bound ? lower_bound : (num > upper_bound ? upper_bound : num)

or if you need it multiple times, monkey-patch the Comparable module:

module Comparable
  def bound(range)
     return range.first if self < range.first
     return range.last if self > range.last
     self
  end
end

so you can use it like

num.bound(lower_bound..upper_bound)

You could also just require ruby facets, which adds a method clip that does just this.

mckeed
Thanks, I might go the Ruby Facets route.
Amy
A: 

Since you're mentioning Rails, I'll mention how to do this with a validation.

validates_inclusion_of :the_column, :in => 5..10

That won't auto-adjust the number, of course.

August Lilleaas