aboutsummaryrefslogtreecommitdiff
path: root/lib/interval.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-12 21:26:19 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-12 21:26:19 +0100
commit51416f5dfac39b6ede8340b57b9bed4eb3edc646 (patch)
treefa840f778dd5a48bc915dd43609d5065b5e1d115 /lib/interval.rb
parentdf7c4a808d691e1d927047bb86aa9738780e694d (diff)
Done Chapter 8
Diffstat (limited to 'lib/interval.rb')
-rw-r--r--lib/interval.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/interval.rb b/lib/interval.rb
index 0aa787f..3ba7e09 100644
--- a/lib/interval.rb
+++ b/lib/interval.rb
@@ -1,7 +1,7 @@
class Interval
def initialize(min = Float::INFINITY, max = -Float::INFINITY)
- @min = min
- @max = max
+ @min = min.to_f
+ @max = max.to_f
end
attr_reader :min, :max
@@ -17,4 +17,14 @@ class Interval
def surround?(x)
min < x && x < max
end
+
+ def sample
+ rand(@min...@max)
+ end
+
+ def clamp(x)
+ return @min if x < @min
+ return @max if x > @max
+ x
+ end
end