lib/rectify.rb in eymiha_math-0.1.0 vs lib/rectify.rb in eymiha_math-0.1.1
- old
+ new
@@ -1,9 +1,18 @@
-# Rectification maps values to an interval treated either as a cycle or a
-# cutoff.
+# Rectification maps values to an limits treated either as cycles, upper
+# bounds, lower bounds or both. Four methods are added to the Numeric class:
+#
+# wrap_rectify() wraps values to an interval treated as a cycle. Think of it
+# as degrees around a compass.
+#
+# cut_rectify() cuts off values to keep them between an upper and lower bound.
+#
+# at_most() cuts off values at an upper bound.
+#
+# at_least() cuts off value at a lower bound.
-# Adds recticiation to Numerics and their progeny
+# Behavior added to Numeric by eymiha_math.
class Numeric
# Treats the interval between low and high as a cycle, returning the result
# of wrapping the instance around this interval.
def wrap_rectify(high=1,low=0)
@@ -19,15 +28,19 @@
# Treats the low and high as limits, returning low or high if the instance
# is respectively below or above these values, or the instance if between
# the two.
def cut_rectify(high=1,low=0)
low,high = high,low if low > high
- if (self < low)
- low
- elsif (self > high)
- high
- else
- self
- end
+ (self < low)? low : (self > high)? high : self
end
+ # Constrains a value to an upper limit.
+ def at_most(high=0)
+ self > high ? high : self
+ end
+
+ # Constrains a value to a lower limit.
+ def at_least(low=0)
+ self < low ? low : self
+ end
+
end