0
返回更好的辦法值我一個方法添加到Numeric
類是這樣的:如何在規定的範圍內使用Ruby
class Numeric
def limitate(min, max)
return self if self.between?(min,max)
return max if self > max
min
end
end
我希望得到一個值,如果值是在指定的範圍內,如果它在範圍之外,它應該返回最大值或最小值。
我可以寫得更簡單嗎?如何?
另請參閱:http://stackoverflow.com/questions/12020787/is-there-a-limit-clamp-function-in-ruby – 2014-08-29 09:44:19