Sha256: 04e6cd942f43db2a91bffb7cae7ead61d054ad40d42a00de0c7d677b9fb438f3

Contents?: true

Size: 872 Bytes

Versions: 15

Compression:

Stored size: 872 Bytes

Contents

module Comparable

  # Returns self if above the given lower bound, or
  # within the given lower and upper bounds,
  # otherwise returns the the bound of which the
  # value falls outside.
  #
  #   4.clip(3)    #=> 4
  #   4.clip(5)    #=> 5
  #   4.clip(2,7)  #=> 4
  #   9.clip(2,7)  #=> 7
  #   1.clip(2,7)  #=> 2
  #
  # CREDIT Florian Gross, Trans

  def clip(lower, upper=nil)
    return lower if self < lower
    return self unless upper
    return upper if self > upper
    return self
  end

  # Returns self if above the given lower bound, or
  # within the given lower and upper bounds,
  # otherwise returns the the bound of which the
  # value falls outside.
  #
  #   4.bound(3)    #=> 4
  #   4.bound(5)    #=> 5
  #   4.bound(2,7)  #=> 4
  #   9.bound(2,7)  #=> 7
  #   1.bound(2,7)  #=> 2
  #
  # CREDIT: Florian Gross

  alias_method :bound, :clip

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
facets-2.9.2 src/core/facets/comparable/clip.rb
facets-2.9.1 lib/core/facets/comparable/clip.rb
facets-2.9.0 lib/core/facets/comparable/clip.rb
facets-2.9.0.pre.2 lib/core/facets/comparable/clip.rb
facets-2.9.0.pre.1 lib/core/facets/comparable/clip.rb
facets-2.8.4 lib/core/facets/comparable/clip.rb
facets-2.8.3 lib/core/facets/comparable/clip.rb
facets-2.8.2 lib/core/facets/comparable/clip.rb
facets-2.8.1 lib/core/facets/comparable/clip.rb
facets-2.8.0 lib/core/facets/comparable/clip.rb
facets-2.7.0 lib/core/facets/comparable/clip.rb
facets-2.6.0 lib/core/facets/comparable/clip.rb
facets-2.5.0 lib/core/facets/comparable/clip.rb
facets-2.5.1 lib/core/facets/comparable/clip.rb
facets-2.5.2 lib/core/facets/comparable/clip.rb