Class: List

Inherits:
Object
  • Object
show all
Defined in:
lib/list.rb

Overview

List

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = []) ⇒ List

Returns a new instance of List.


7
8
9
# File 'lib/list.rb', line 7

def initialize(items = [])
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.


5
6
7
# File 'lib/list.rb', line 5

def items
  @items
end

Instance Method Details

#add(item) ⇒ Object


11
12
13
14
15
# File 'lib/list.rb', line 11

def add(item)
  raise 'Already exists in list' if found?(item)
  @items << item
  'Success'
end

#find(item) ⇒ Object


35
36
37
# File 'lib/list.rb', line 35

def find(item)
  all.detect { |arr| arr == item }
end

#get_by_index(index) ⇒ Object


31
32
33
# File 'lib/list.rb', line 31

def get_by_index(index)
  all[index]
end

#remove(item) ⇒ Object


17
18
19
20
21
# File 'lib/list.rb', line 17

def remove(item)
  raise 'Not found in list' unless found?(item)
  @items.delete(item)
  'Success'
end

#to_string(item_list_printer = ItemListPrinter) ⇒ Object


23
24
25
# File 'lib/list.rb', line 23

def to_string(item_list_printer = ItemListPrinter)
  item_list_printer.to_string(all)
end

#to_summary(keys, item_list_printer = ItemListPrinter) ⇒ Object


27
28
29
# File 'lib/list.rb', line 27

def to_summary(keys, item_list_printer = ItemListPrinter)
  item_list_printer.to_summary(all, keys)
end