class Sleep2

Constants

VERSION

Attributes

duration[RW]
new[W]
sleep_proc[RW]

Public Class Methods

[](duration) click to toggle source
# File lib/sleep2.rb, line 6
def self.[] duration
  new duration
end
new(time_interval) click to toggle source
# File lib/sleep2.rb, line 13
def initialize time_interval
  if time_interval.class.ancestors.include?(Numeric)
    self.duration = time_interval.to_f
  elsif time_interval.class == self.class
    self.duration = time_interval.duration.to_f
  else
    msg = "can't convert #{time_interval.class} into time interval"
    raise TypeError, msg
  end
  @new = true
end

Public Instance Methods

<=>(other) click to toggle source

Makes sleep instance comparable with another object of its type and integer

# File lib/sleep2.rb, line 52
def <=>(other)
  if other.kind_of? self.class
    self.duration <=> other.duration
  elsif other.kind_of? Integer
    self.duration <=> other
  end
end
[](new_duration) click to toggle source
# File lib/sleep2.rb, line 39
def [] new_duration
  self.duration = new_duration
end
inspect() click to toggle source
# File lib/sleep2.rb, line 29
def inspect
  # Avoids sleeping the first time it is called (the moment of instantiation)
  unless new?
    self.sleep_proc.call
  else
    self.new        = false
    self.sleep_proc = proc { sleep duration }
  end
end
new?() click to toggle source
# File lib/sleep2.rb, line 25
def new?
  @new
end