aboutsummaryrefslogtreecommitdiff
path: root/lib/interval.rb
blob: 3ba7e09a3ca29497bcf0fb7db3537262821a054f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Interval
  def initialize(min = Float::INFINITY, max = -Float::INFINITY)
    @min = min.to_f
    @max = max.to_f
  end

  attr_reader :min, :max

  def size
    @max - @min
  end

  def include?(x)
    min <= x && x <= max
  end

  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