Class: Sleep2
- Inherits:
-
Object
- Object
- Sleep2
- Includes:
- Comparable
- Defined in:
- lib/sleep2.rb,
lib/sleep2/version.rb
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#new ⇒ Object
writeonly
Sets the attribute new.
-
#sleep_proc ⇒ Object
Returns the value of attribute sleep_proc.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Makes sleep instance comparable with another object of its type and integer.
- #[](new_duration) ⇒ Object
-
#initialize(time_interval) ⇒ Sleep2
constructor
A new instance of Sleep2.
- #inspect ⇒ Object
- #new? ⇒ Boolean
Constructor Details
#initialize(time_interval) ⇒ Sleep2
Returns a new instance of Sleep2.
13 14 15 16 17 18 19 20 21 22 23 |
# 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 |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
10 11 12 |
# File 'lib/sleep2.rb', line 10 def duration @duration end |
#new=(value) ⇒ Object (writeonly)
Sets the attribute new
11 12 13 |
# File 'lib/sleep2.rb', line 11 def new=(value) @new = value end |
#sleep_proc ⇒ Object
Returns the value of attribute sleep_proc.
10 11 12 |
# File 'lib/sleep2.rb', line 10 def sleep_proc @sleep_proc end |
Class Method Details
.[](duration) ⇒ Object
6 7 8 |
# File 'lib/sleep2.rb', line 6 def self.[] duration new duration end |
Instance Method Details
#<=>(other) ⇒ Object
Makes sleep instance comparable with another object of its type and integer
52 53 54 55 56 57 58 |
# 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) ⇒ Object
39 40 41 |
# File 'lib/sleep2.rb', line 39 def [] new_duration self.duration = new_duration end |
#inspect ⇒ Object
29 30 31 32 33 34 35 36 37 |
# 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? ⇒ Boolean
25 26 27 |
# File 'lib/sleep2.rb', line 25 def new? @new end |