Module: Comparable
- Included in:
- Symbol
- Defined in:
- lib/backports/2.4.0/comparable/clamp.rb,
lib/backports/2.7.0/comparable/clamp.rb
Instance Method Summary collapse
Instance Method Details
#clamp(min, max) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/backports/2.4.0/comparable/clamp.rb', line 5 def clamp(min, max) if Backports.coerce_to_comparison(min, max) > 0 raise ArgumentError, "min argument must be smaller than max argument" end case Backports.coerce_to_comparison(self, min) when 0 self when -1 min else self > max ? max : self end end |
#clamp_with_range(range_or_min, max = Backports::Undefined) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/backports/2.7.0/comparable/clamp.rb', line 8 def clamp_with_range(range_or_min, max = Backports::Undefined) return clamp_without_range(range_or_min, max) unless max == Backports::Undefined raise TypeError, "wrong argument type #{range_or_min.class} (expected Range)" unless range_or_min.is_a?(Range) if range_or_min.end.nil? # 2.6's endless range self < range_or_min.begin ? range_or_min.begin : self elsif range_or_min.exclude_end? raise ArgumentError, 'cannot clamp with an exclusive range' else clamp_without_range(range_or_min.begin, range_or_min.end) end end |