Sha256: b21cc5d8126de57fed3deecd4bee59314e11b1a4f7523b58aa16a4691679b6df

Contents?: true

Size: 419 Bytes

Versions: 14

Compression:

Stored size: 419 Bytes

Contents

module MoreCoreExtensions
  module NumericClamp
    #
    # Clamp a number to a minimum and/or maximum value.
    #
    #   8.clamp(nil, nil) #=> 8
    #   8.clamp(9, nil)   #=> 9
    #   8.clamp(nil, 6)   #=> 6
    def clamp(min, max)
      value = self
      value = [value, min].max if min
      value = [value, max].min if max
      value
    end
  end
end

Numeric.send(:prepend, MoreCoreExtensions::NumericClamp)

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
more_core_extensions-4.5.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.4.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.3.1 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.3.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.2.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.1.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-4.0.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.8.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.7.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.6.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.5.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.4.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.3.0 lib/more_core_extensions/core_ext/numeric/clamp.rb
more_core_extensions-3.2.0 lib/more_core_extensions/core_ext/numeric/clamp.rb