Class: Taskinator::Tasks
- Inherits:
-
Object
- Object
- Taskinator::Tasks
- Includes:
- Enumerable
- Defined in:
- lib/taskinator/tasks.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
(also: #length)
readonly
Returns the value of attribute count.
-
#head ⇒ Object
(also: #first)
readonly
implements a linked list, where each task references the next task.
Instance Method Summary collapse
- #add(task) ⇒ Object (also: #<<, #push)
- #attach(task, count) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(first = nil) ⇒ Tasks
constructor
A new instance of Tasks.
- #inspect ⇒ Object
Constructor Details
#initialize(first = nil) ⇒ Tasks
Returns a new instance of Tasks.
13 14 15 16 |
# File 'lib/taskinator/tasks.rb', line 13 def initialize(first=nil) @count = 0 add(first) if first end |
Instance Attribute Details
#count ⇒ Object (readonly) Also known as: length
Returns the value of attribute count.
10 11 12 |
# File 'lib/taskinator/tasks.rb', line 10 def count @count end |
#head ⇒ Object (readonly) Also known as: first
implements a linked list, where each task references the next task
7 8 9 |
# File 'lib/taskinator/tasks.rb', line 7 def head @head end |
Instance Method Details
#add(task) ⇒ Object Also known as: <<, push
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/taskinator/tasks.rb', line 24 def add(task) if @head.nil? @head = task @count = 1 else current = @head while current.next current = current.next end current.next = task @count += 1 end task end |
#attach(task, count) ⇒ Object
18 19 20 21 22 |
# File 'lib/taskinator/tasks.rb', line 18 def attach(task, count) @head = task @count = count task end |
#each(&block) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/taskinator/tasks.rb', line 46 def each(&block) return to_enum(__method__) unless block_given? current = @head while current yield current current = current.next end end |
#empty? ⇒ Boolean
42 43 44 |
# File 'lib/taskinator/tasks.rb', line 42 def empty? @head.nil? end |
#inspect ⇒ Object
56 57 58 |
# File 'lib/taskinator/tasks.rb', line 56 def inspect %([#{collect(&:inspect).join(', ')}]) end |