Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 52 lines (45 sloc) 0.717 kb
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
class Class
  def direct_descendents
    result = []
    ObjectSpace.each_object( Class ) do |c|
      result << c if c.superclass == self
    end
    return result
  end
end

module NamedRandom
  def randomly_between(range)
    range.begin + rand*(range.end-range.begin)
  end
  
  def with_probability(p)
    yield if (rand <= p)
  end
end

module NamedFractions
  def half
    0.5
  end
  
  def third
    1.0/3
  end
  
  def quarter
    0.25
  end
end

module Timer
  include NamedFractions
  def after(duration)
    Thread.new do
      sleep duration.to_f
      yield
    end
  end
  
  def every(duration)
    Thread.new do
      while true
        yield
        sleep duration.to_f
      end
    end
  end
end
Something went wrong with that request. Please try again.