Sha256: c559eaef411e8d5b38c683188f6c76f6692525fda98553a632369ece8c632302

Contents?: true

Size: 802 Bytes

Versions: 1

Compression:

Stored size: 802 Bytes

Contents

#--
# Credit goes to Florian Gross.
#++
class Numeric
  # Returns the value or the given upper or lower bound
  # if the value falls outside of them.
  #
  #   require 'facet/comparable/bound_by'
  #
  #   4.bound_by(2,7)  #=> 4
  #   9.bound_by(2,7)  #=> 7
  #   1.bound_by(2,7)  #=> 2
  #
  def bound_by(lower, upper)
    return lower if self < lower
    return upper if self > upper
    return self
  end
  
  # Returns the lower of self or x.
  #
  #   require 'facet/numeric/bound_by'
  #
  #   4.at_least(5)  #=> 5
  #   6.at_least(5)  #=> 6
  #
  def at_least(x)
    (self >= x) ? self : x
  end
  
  # Returns the greater of self or x.
  #
  #   require 'facet/numeric/bound_by'
  #
  #   4.at_most(5)  #=> 4
  #   6.at_most(5)  #=> 5
  #
  def at_most(x)
    (self <= x) ? self : x
  end
end 

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/numeric/bound_by.rb