Ruby Elegant Way to Return Min/Max if Value Outside Range
ruby
Solution
If it is just for one occasion, I would recommend Shawn Balestracci's answer, which is the most beautiful.
Alternatively, here are some methods from my personal library:
module Comparable
def at_least other; self < other ? other : self end
def at_most other; self > other ? other : self end
end
I use it like this:
fee_amount = <whatever logic I need>.at_least(min).at_most(max)
Problem
So I am writing a program to model a process and need to calculate fees. The logic is that if the fee amount is less than a minimum use the minimum and if the fee amount is greater than the maximum use the maximum. I can of course make this happen on multiple lines but would be interested to know if there is a more elegant way to do this in Ruby. ``` fee_amount = <whatever logic I need> if fee_amount < min return min if fee_amount > max return max return fee_amount ```