Sha256: e1704afc0dfbe3eadb35cb97cde605fbe85bd8a2b5ddb44a1647ccb7cc015c07

Contents?: true

Size: 893 Bytes

Versions: 19

Compression:

Stored size: 893 Bytes

Contents

require 'backports/2.4.0/comparable/clamp' unless Comparable.method_defined? :clamp

if Comparable.instance_method(:clamp).arity == 2
  require 'backports/tools/alias_method_chain'
  require 'backports/tools/arguments'

  module Comparable
    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

    Backports.alias_method_chain self, :clamp, :range
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
backports-3.25.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.24.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.24.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.23.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.22.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.22.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.21.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.20.2 lib/backports/2.7.0/comparable/clamp.rb
backports-3.20.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.20.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.19.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.18.2 lib/backports/2.7.0/comparable/clamp.rb
backports-3.18.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.18.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.17.2 lib/backports/2.7.0/comparable/clamp.rb
backports-3.17.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.17.0 lib/backports/2.7.0/comparable/clamp.rb
backports-3.16.1 lib/backports/2.7.0/comparable/clamp.rb
backports-3.16.0 lib/backports/2.7.0/comparable/clamp.rb