Class: Taskinator::Tasks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/taskinator/tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first = nil) ⇒ Tasks

Returns a new instance of Tasks.



10
11
12
# File 'lib/taskinator/tasks.rb', line 10

def initialize(first=nil)
  @head = first
end

Instance Attribute Details

#headObject (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



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/taskinator/tasks.rb', line 14

def add(task)
  if @head.nil?
    @head = task
  else
    current = @head
    while current.next
      current = current.next
    end
    current.next = task
  end
  task
end

#each(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/taskinator/tasks.rb', line 34

def each(&block)
  return to_enum(__method__) unless block_given?

  current = @head
  while current
    yield current
    current = current.next
  end
end

#empty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/taskinator/tasks.rb', line 30

def empty?
  @head.nil?
end

#inspectObject



44
45
46
# File 'lib/taskinator/tasks.rb', line 44

def inspect
  %([#{collect(&:inspect).join(', ')}])
end