From 51416f5dfac39b6ede8340b57b9bed4eb3edc646 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Wed, 12 Jun 2024 21:26:19 +0100 Subject: Done Chapter 8 --- lib/interval.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/interval.rb') 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 -- cgit v1.2.1